我有一组扩展名为.dat的ascii文件,我需要将它们转换为一组.xml文件。
无论如何都可以使用 Matlab 或任何其他软件来做到这一点。
这是我需要转换的文件之一:
https://docs.google.com/open?id=0B1GI9KuZUKX3TDFCZDVTbzdINUE
我过去用过XML4MAT。 它将处理进出XML格式的数据转换,但不能完全处理实际读取和写入XML文件,因此您必须添加一些粘合代码。 顺序为:
- 将 dat 文件读入 MATLAB 中的一个变量(这里我使用变量名称 Data)。 看起来您的文件本质上是一个数字表,所以这很容易。
-
使用 DumpToXML.m 和 LoadFromXML.m 作为将单独下载的XML4MAT包的粘附代码。
% 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
我不认为Matlab是首选的武器。
我会提倡Python,因为有很好的XML包,如lxml。您应该能够使用 open() 和 readlines() 轻松解析 dat 文件。