read a formated text
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
dpb
on 12 Dec 2014
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]
>>
More Answers (2)
Thorsten
on 15 Dec 2014
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
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!