Textscan read too many lines
이전 댓글 표시
I want to read XML file that contain few data sections (xml file of spectrometer with more than one measure point)
The data structure is as follows: (more or less)
<SpectrumData>
2. <X>50000</X>
3. <Y>54000</Y>
4. <Z>75000</Z>
5. </ActualInfo>
6. <I WL="235" R="0.101973" />
7. <I WL="236" R="0.098217" />
8. <I WL="237" R="0.093565" />
…
731. <I WL="968" R="0.158408" />
732. <I WL="969" R="0.158925" />
733. </SpectrumData>
734. <SpectrumData>
735. <X>100</X>
736. <Y>120</Y>
737. <Z>250</Z>
738. </ActualInfo>
739. <I WL="235" R="0.201973" />
740. <I WL="236" R="0.198217" />
741. <I WL="237" R="0.193565" />
…
1472. <I WL="968 " R="0.158408" />
1473. </SpectrumData>
This structure repeat itself two to six times (depend on the number of measured points)
First I used
tline = fgetl(fid);
SpectrumData = textscan(fid,'%*s %f %*s',3,'delimiter','>');
To read X,Y,Z And then I use
C = textscan(fid,'%*s %f %*s %f %*s', 'delimiter','"');
To read all WL and R
The problem is textscan read 3 more lines after the last WL (and the last cells in C are NaN) How can I stop textscan in the correct line, note that I can’t use
C = textscan(fid,'%*s %f %*s %f %*s', 733,'delimiter','"');
Because the number of WL can be change from file to file
댓글 수: 1
dpb
2014년 9월 27일
Unless there's a piece of data somewhere in the file that contains the number of lines so you can set the repeat count in textscan programmatically, you can't do anything specific to the count a priori.
Probably there's a regexp pattern that could be written to find the terminator but I'm not expert enough to work it out w/o a fair amount of effort so won't give a go.
Otherwise, either
a) continue to read/parse the file line-by-line looking for the termination string, or
b) read the whole file into memory as array of cell strings then locate the sections by string comparison operations and operate on those in memory.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!