Gamba Osaca
Gamba Osaca

Reputation: 307

convert ascii .dat files to .xml using Matlab (or any other software)

I have a set of ascii files with the extension .dat and I need to convert them into a set of .xml files.

Is there anyway to do this with Matlab or any other software.

This is one of the files that I need to convert:

https://docs.google.com/open?id=0B1GI9KuZUKX3TDFCZDVTbzdINUE

Upvotes: 0

Views: 2510

Answers (2)

ZSG
ZSG

Reputation: 1369

I've used XML4MAT in the past. It will handle the data conversion into and out of an XML format, but doesn't quite handle actually reading and writing the XML file, so you have to add a little glue code. The sequence is:

  1. Read the dat file into one variable in MATLAB (here I use the variable name Data). It looks like your file is essentially a table of numbers so that is easy.
  2. Use DumpToXML.m and LoadFromXML.m as the glue code to the XML4MAT package that you'll download separately.

    % function DumpToXML(XMLFileName, Data)
    function DumpToXML(XMLFileName, Data)
    
        % Generate the text of the XML file.
        XMLData = ['<root>' 10];
        XMLData = [XMLData mat2xml(Data, 'Data', 1)];
        XMLData = [XMLData '</root>' 0];
    
        % Now output the data.
        fid = fopen(XMLFileName, 'w');
        fprintf(fid, '%s', XMLData);
        fclose(fid);
    end
    
    
    % function LoadFromXML(XMLFileName)
    function Data = LoadFromXML(XMLFileName)
    
        % Open the XML file.
        fid = fopen(XMLFileName, 'r');
        if(fid <= 0)
            error(['Cannot open XML file ' XMLFileName]);
        end
        XMLData = fscanf(fid, '%c', inf);
        fclose(fid);
    
        % Now get the Data tag.
        DataStartIndex = findstr(XMLData, '<Data');
        % Now find the end.
        DataEndIndex = findstr(XMLData, '</Data>');
    
        % Extract the strings for this two variable from the string
        % containing the loaded XML file.
        XMLData = XMLData(DataStartIndex:DataEndIndex+6);
    
        % And turn it back into a variable.
        Data = xml2mat(XMLData);
    end
    

Upvotes: 1

user1720740
user1720740

Reputation: 848

I don't think Matlab is the weapon of choice for that.

I'd advocate for Python since there are nice XML packages such as lxml. You should be able to parse the dat file easily with open() and readlines().

Upvotes: 0

Related Questions