Assume that your data is available in arraysarrData1andarrData2. Create an XML document node, sayNode1as follows:
docNode = com.mathworks.xml.XMLUtils.createDocument('Node1');
docRootNode = docNode.getDocumentElement;
Now put the data in the data nodes..
for i=1:length(arrData1),
% create nodes..
elPar = docNode.createElement('DataParent');
elData1 = docNode.createElement('Data1');
elData2 = docNode.createElement('Data2');
% put data in nodes..
elData1.appendChild(...
docNode.createTextNode(sprintf('%f', arrData1(i))));
elData2.appendChild(...
docNode.createTextNode(sprintf('%f', arrData2(i))));
% put nodes in the correct positions..
elParent.appendChild(elData1);
elParent.appendChild(elData2);
docRootNode.appendChild(elParent);
end
Now save the XML document. You can save it directly, or useuiputfileto get the standard save dialog of the OS.
[filename, pathname] = uiputfile(...
sprintf('%s.xml', datFile), 'Save XML file as');
% Save the XML document.
% xmlFileName = [dataOutFile,'.xml'];
xmlFileName = fullfile(pathname, filename);
xmlwrite(xmlFileName,docNode);
% open xml file to see your output
edit(xmlFileName);
To see more ways of using XML with MATLAB, refer to Java's documentation and play around to see what works.
Wednesday, December 22, 2004
Creating XML outputs with MATLAB
If you want to format your data output as an XML file, here's how to do it without doing any complicated string handling. MATLAB can use the XML handling capabilities of Java very easily. Here's how:
The MATLAB blog
Over the course of my research and work I have accumulated a fair share of MATLAB hacks where I have used MATLAB to accomplish tasks that go beyond the usual equation-solving and numerical-simulations routines. Mostly I have been motivated by my own laziness and reluctance to code everything in C, or to use a combination of tools, but as I have discovered along the way, MATLAB is truly a very powerful tool and allows you to accomplish complicated stuff using very simple commands. If you have any interesting ways of using MATLAB, feel free to send them in and I will duly acknowledge your contribution. Enjoy!
Subscribe to:
Posts (Atom)
Moving to a new website
This website was intended to be a little collection of tips and tricks, but I haven't been able to update it as often as I originally in...
-
Over the course of my research and work I have accumulated a fair share of MATLAB hacks where I have used MATLAB to accomplish tasks that go...
-
It is an easy matter to save data as a mat(MATLAB binary) file or an ASCII file so long as you know the filename beforehand. For ex: to save...
-
If you want to show how your plots change with time, a movie is a good idea. Especially since MATLAB can produce movies that can be embedded...