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

时间:2011-07-15 12:31:34

标签: matlab file-io

我是一个完整的编程初学者,正在尝试学习MATLAB。我想从一堆不同的xml文件中提取数值数据。数字数据项由标签和边界限定。如何在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

2 个答案:

答案 0 :(得分:8)

MATLAB中读取xml数据的基本功能是xmlread;但如果你是一个完全的初学者,那么使用它可能会很棘手。试试this series of blog postings,向您展示如何将它们放在一起。

答案 1 :(得分: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));