2011-07-15 115 views
5

我是一個試圖學習MATLAB的完整編程初學者。我想從一堆不同的xml文件中提取數字數據。數字數據項由標籤和邊界限定。我如何在MATLAB中編寫程序?使用MATLAB從xml文件中提取數據

我的算法:

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 
+0

可能重複:HTTP://計算器.COM /問題/ 6582250 /提取數據之間兩標籤功能於HTML文件,MATLAB – Amro

回答

8

在MATLAB的基本函數來讀取XML數據xmlread;但是如果你是一個完整的初學者,那麼使用它可能會很棘手。試試this series of blog postings,告訴你如何把它放在一起。

1

假設你想閱讀本文件:

<PositiveSamples numImages="14"> 
 
<image numSubRegions="2" filename="TestingScene.jpg"> 
 
\t <subregion yStart="213" yEnd="683" xStart="1" xEnd="236"/> 
 
\t <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(numImages); 
numOfImages = uint16(eval(numImages));