Can I return the xml string from writestruct?

I am successfully using the "writestruct()" function to export my structure to XML but due to the application this xml is being read into afterwards I need to do a bit of tweaking to the output file contents.
Is there a way to return the generated XML string rather than have Matlab write it to file? I can fopen/fprintf my modifed xml string easily enough afterwards.

2 Kommentare

Stephen23
Stephen23 vor etwa eine Stunde
writestruct does not provide an option to return the generated XML as a string.
Martin Dale
Martin Dale vor etwa eine Stunde
yes, I know that the writestruct function does not naturally return the xml string but was hoping to find out if there was a matlab internal function somewhere within matlab.io.xml.internal that does return the xml string.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt J
Matt J vor etwa eine Stunde
Bearbeitet: Matt J vor etwa eine Stunde

0 Stimmen

You can't suppress file output, but you can use readlines and writelines to retrieve the file contents in string form and then modify them.

Weitere Antworten (1)

Umar
Umar vor etwa 9 Stunden
Bearbeitet: Umar vor etwa 9 Stunden

0 Stimmen

Hi @Martin Dale,
Just to add a bit more to this — I dug through the docs and Matt J's right, writestruct doesn't have any hidden option to spit out the XML as a string, it's file-output only. But I put together a short PDF that walks through both options in more detail: the readlines/writelines round-trip Matt mentioned, plus an alternative if you'd rather not touch a file at all (building the XML yourself with the DOM API and pulling the string straight out of xmlwrite).
Attaching it here in case the code samples are easier to follow laid out like this rather than scattered across comments. Should be a quick read — hope it helps sort out the tweaking you need to do before it gets read into your other application.

Produkte

Version

R2025b

Gefragt:

am 27 Jul. 2026 um 12:22

Bearbeitet:

vor etwa 9 Stunden

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by