read a formated text

2 次查看(过去 30 天)
Song
Song 2014-12-12
评论: Song 2014-12-16
How can I read the Information from the following text file?
<content d="2000-11-02" o="21.410" h="21.800" c="21.600" l="21.310" v="21014" bl="" />
<content d="2000-11-03" o="21.610" h="21.680" c="21.420" l="21.380" v="7412" bl="" />
<content d="2000-11-06" o="21.420" h="21.450" c="21.370" l="21.330" v="8795" bl="" />
<content d="2000-11-07" o="21.380" h="21.500" c="21.360" l="21.280" v="9994" bl="" />
What interests me are
"2000-11-06", 21.420, 21.450, 21.370, 21.330, 8795
I tried
textscan(fid, '\t<content d="%4d-%2d-%2d" o="%f" h="%f" c="%f" l="%f" v="%f" bl="" />')
or
textscan(fid, '\t<content d="%q" o="%f" h="%f" c="%f" l="%f" v="%f" bl="" />')
But it doesn't work. Anyone can help?
BTW, converting dates using
dates = cellfun(@(x)datenum(x, 'yyyy-mm-dd'), dates);
is kind of slow. Anyone knows a faster way?

采纳的回答

dpb
dpb 2014-12-12
Forget the '\t' in the first format string; textscan uses it as one of the default delimiters, anyway.
I pasted one line in to command window --
textscan(s, '<content d="%4d-%2d-%2d" o="%f" h="%f" c="%f" l="%f" v="%f" bl="" />')
ans =
[2000] [11] [2] [21.4100] [21.8000] [21.6000] [21.3100] [21014]
>>
  1 个评论
Song
Song 2014-12-15
S = ['<?xml version="1.0" encoding="UTF-8"?> />' sprintf('\n') ...
'<control> />' sprintf('\n') ...
' <content d="2000-01-04" o="24.980" h="25.780" c="25.570" l="24.750" v="44961" bl="" />' sprintf('\n') ...
' <content d="2000-01-05" o="25.570" h="25.980" c="25.280" l="25.150" v="52528" bl="" />' sprintf('\n') ...
' <content d="2000-01-06" o="25.180" h="26.300" c="25.990" l="25.050" v="62297" bl="" />' sprintf('\n') ...
' <content d="2000-01-07" o="26.300" h="27.500" c="26.900" l="26.120" v="213553" bl="" />' sprintf('\n') ...
' <content d="2000-01-10" o="27.000" h="27.850" c="27.250" l="26.710" v="165397" bl="" />' sprintf('\n') ...
' <content d="2000-01-11" o="27.250" h="27.300" c="26.200" l="26.120" v="93908" bl="" />' sprintf('\n') ...
' <content d="2000-01-12" o="26.000" h="26.000" c="25.120" l="24.800" v="352749" bl="" />' sprintf('\n') ...
' <content d="2000-01-13" o="25.000" h="25.250" c="24.900" l="24.800" v="79756" bl="" />' sprintf('\n') ...
' <content d="2000-01-14" o="24.880" h="25.000" c="24.200" l="23.910" v="178619" bl="" />' sprintf('\n') ...
' <content d="2000-01-17" o="24.080" h="24.440" c="24.400" l="23.750" v="81015" bl="" />' sprintf('\n') ...
' <content d="2000-01-18" o="24.500" h="24.570" c="24.140" l="23.880" v="76933" bl="" />' sprintf('\n') ...
' <content d="2000-01-19" o="24.140" h="24.290" c="24.130" l="23.980" v="46584" bl="" />' sprintf('\n') ...
' <content d="2000-01-20" o="24.120" h="24.650" c="24.440" l="24.100" v="51144" bl="" />' sprintf('\n') ...
'</control>'];
textscan(S, '<content d="%4d-%2d-%2d" o="%f" h="%f" c="%f" l="%f" v="%f" bl="" />')
I tried your code, but still not working ...

请先登录,再进行评论。

更多回答(2 个)

Song
Song 2014-12-15
As an example to test:
S = ['<?xml version="1.0" encoding="UTF-8"?> />' sprintf('\n') ...
'<control> />' sprintf('\n') ...
' <content d="2000-01-04" o="24.980" h="25.780" c="25.570" l="24.750" v="44961" bl="" />' sprintf('\n') ...
' <content d="2000-01-05" o="25.570" h="25.980" c="25.280" l="25.150" v="52528" bl="" />' sprintf('\n') ...
' <content d="2000-01-06" o="25.180" h="26.300" c="25.990" l="25.050" v="62297" bl="" />' sprintf('\n') ...
' <content d="2000-01-07" o="26.300" h="27.500" c="26.900" l="26.120" v="213553" bl="" />' sprintf('\n') ...
' <content d="2000-01-10" o="27.000" h="27.850" c="27.250" l="26.710" v="165397" bl="" />' sprintf('\n') ...
' <content d="2000-01-11" o="27.250" h="27.300" c="26.200" l="26.120" v="93908" bl="" />' sprintf('\n') ...
' <content d="2000-01-12" o="26.000" h="26.000" c="25.120" l="24.800" v="352749" bl="" />' sprintf('\n') ...
' <content d="2000-01-13" o="25.000" h="25.250" c="24.900" l="24.800" v="79756" bl="" />' sprintf('\n') ...
' <content d="2000-01-14" o="24.880" h="25.000" c="24.200" l="23.910" v="178619" bl="" />' sprintf('\n') ...
' <content d="2000-01-17" o="24.080" h="24.440" c="24.400" l="23.750" v="81015" bl="" />' sprintf('\n') ...
' <content d="2000-01-18" o="24.500" h="24.570" c="24.140" l="23.880" v="76933" bl="" />' sprintf('\n') ...
' <content d="2000-01-19" o="24.140" h="24.290" c="24.130" l="23.980" v="46584" bl="" />' sprintf('\n') ...
' <content d="2000-01-20" o="24.120" h="24.650" c="24.440" l="24.100" v="51144" bl="" />' sprintf('\n') ...
'</control>'];
textscan(S, '<content d="%4d-%2d-%2d" o="%f" h="%f" c="%f" l="%f" v="%f" bl="" />')

Thorsten
Thorsten 2014-12-15
i1 = findstr(S, '<content');
i2 = findstr(S, '/>');
i2 = i2(find(i2>i1(1), 1, 'first'):end) + 1;
for i=1:numel(i1)
R(i,:) = textscan(S(i1(i):i2(i)), '<content d="%4d-%2d-%2d" o="%f" h="%f" c="%f" l="%f" v="%f" bl="" />')
end

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by