使用MATLAB从XML文件中提取数据

5

我是一个完全的编程初学者,试图学习MATLAB。我想从一堆不同的xml文件中提取数字数据。 数值数据项由标签<Data>和</Data>限定。 我该如何在MATLAB中编写程序?

我的算法:

1. Open the folder
2. Look into each of 50 xml files, one at a time
3. Where the tag <HNB.1></HNB.1> exists, copy numerical contents between said tag and write results into a new file
4. The new file name given for step 3 should be the same as the initial file name read in Step 2, being appended with "_data extracted"

例子:

FileName = Stewart.xml
Contents = blah blah blah <HNB.1>2</HNB.1> blah blah
NewFileName = Stewart_data extracted.txt
Contents = 2

可能重复:http://stackoverflow.com/questions/6582250/extracting-data-between-two-tags-in-html-file-matlab - Amro
2个回答

8
MATLAB中读取xml数据的基本函数是xmlread;但如果你是一个完全的初学者,仅使用该函数可能会有些棘手。尝试这个博客系列文章,它将向您展示如何将所有内容组合在一起。

1

假设您想要阅读这个文件:

<PositiveSamples numImages="14">
<image numSubRegions="2" filename="TestingScene.jpg">
 <subregion yStart="213" yEnd="683" xStart="1" xEnd="236"/>
 <subregion yStart="196" yEnd="518" xStart="65" xEnd="226"/>
</image>
</PositiveSamples>

在matlab中,按如下方式读取文件内容:
%read xml file
xmlDoc = xmlread('PositiveSamples.xml');

%Get root element
root = xmlDoc.getDocumentElement();

%Read attributevale
numOfImages = root.getAttribute('numImages');
numOfImages = char(numOfImages);   
numOfImages = uint16(eval(numOfImages));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接