• Search
  • Register
  • Log in
  • The Math Root » Matlab
  • HOWTO: Creating XML output with Matlab
  •  
    Anil

    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:

    Assume that your data is available in arrays arrData1 and arrData2. Create an XML document node, say Node1 as 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 use uiputfile to 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.

    #6
    Anil
    Member
    Posted 1 year ago
     
    Glavina

    I have one doubt here: what is the size limit to create a xml function this way.
    I tried to use this technique, storing the value of 25 vectors (it would create a type of matrix 9x25), but its not working. I think its because of the method to do it, "<?xml version="1.0" encoding="utf-8"?>", but there's a way to do it differently?

    Pls, any help?

    #11
    Glavina
    Member
    Posted 1 year ago
    RSS feed for this topic  

    Reply

    You must log in to post.

      Tags:

  •  © 2009 Anil Kandangath. All posts are owned by the whoever posted them. Share your knowledge.
    The Math Root is proudly powered by bbPress.. This site is not affiliated with The MathWorks™ or Wolfram Research. //   Theme by Mike Lothar  
    [ Time : 0.110s | 8 Queries ]