Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8388799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:33:03+00:00 2026-06-09T18:33:03+00:00

I would like to read an XML file and modify some strings, save then

  • 0

I would like to read an XML file and modify some strings, save then close the file with MATLAB. So far I have:

f = fopen( 'output_results\results.xml', 'w' ); 

I need to add the following line inside the optList node of the file (see below):

<opt name="*_option1">true</opt>
<opt name="format">
    <f1>file:/C:/working/types.h</f1>
</opt>

save then close the file

fclose(f); 

How can I add the above lines in an XML file?

File content:

<?xml version="1.0" encoding="utf-8"?> 
<Custom_project name="" val="True" name="file1" path="file:/C:/Users/Local/Temp/info.xml" version="1.0">
<verif="true" name="values" path="file:/C:/Users/Temp/folder1">
    <optList name="values">
        <opt name="color">red</opt>
        <opt name="police">calibri</opt>
        <opt name="font">blue</opt>
    </optList>
</verif>
<toto="myvalue" name="option1">
    <opt name="myvalue_1">32</opt>
    <opt name="-total">All</opt> 
    <opt name="characteristic">hybrid</opt>
</toto> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:33:04+00:00Added an answer on June 9, 2026 at 6:33 pm

    As found here, it’s not possible to open a file, seek a location, add stuff there, while preserving the rest of the text, and close.

    You can work around this simply by re-writing the entire file:

    f = fopen( 'output_results\results.xml', 'r' ); 
    g = fopen( 'output_results\results.xml.TEMP', 'w' ); 
    
    while ~feof(f)
        line = fgets(f);
        fprintf(g, '%s', line);
        if strcmpi(line, '<optList name="values">')
            fprintf(g, '%s\n%s\n%s\n%s\n',....
                '<opt name="*_option1">true</option>',...
                '<opt name="format">',...
                '<f1>file:/C:/working/types.h',...
                '</f1></option>');
        end
    end
    
    fclose(f), fclose(g);
    movefile('output_results\results.xml.TEMP', 'output_results\results.xml');
    

    If this is really a one-off problem, the hack above is OK. But, as suggested by @bdecaf, you should use the proper tool for the job. I’d suggest to do the writing entirely outside of MATLAB (to avoid overly complex code), and just call an external tool/library via MATLAB’s system call syntax (type help !).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read multiple XML files in an array and then parse
I would like to read an xml file. I' ve found an example which
I have got a huge xml file (~ 85 Mo) and I would like
I would like to sign an XML file I have created, and have followed
Hi there I have been trying to read an xml file with UTF strings,
I would like to locally save an XML file with ActionScript, as a temp
Got an application wich read some infos in a specific XML file. I would
I have some xml files that have different elements. I would like to write
I would like to read a text file and input its contents into an
I would like to read a DICOM file in C#. I don't want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.