read a formated text
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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]
>>
更多回答(2 个)
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 Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!