How to change textscan to fscanf?

Hello,
I was just needing some help changing this textscan code to fscanf,
I know I need to add a range at the end ([1,inf]), but even after that i cant get it to work.
%opens and searches for a file under the variable name
fileID = fopen(filename);
%While loop scans lines for our desired text and filters out unwanted text until end of file
while ~feof(fileID)
d = fgetl(fileID);
Latitude = textscan(d,'<trkpt lat="%f" lon="%f">\n');
Longitude = textscan(d, 'long="%f">\n');
Elevation = textscan(d, '<ele>%f</ele>)\n');
Time = textscan (d, '<time>2020-02-01T%f:%f:%f3z</time>\n');
%sorts the extracted data into columns
mat = [mat; Latitude, Longitude, Elevation, Time];
end
Thanks

 Accepted Answer

You need a completely different implementation.
fopen() the file. Then, repeatedly fgetl() one line. Check if the line begins with <trkpt . If it does, use sscanf() to read the lat and lon and record those. Otherwise check if the line begins with <time> and if it does then extract the time using text manipulation and probably call datetime() to convert it to a datetime object to record it. Otherwise check if the line begins with <ele> and if it does then sscanf() to read the elevation, and record it. If none of those match, ignore the line.
Remember to fclose() the file afterwards.

2 Comments

Hint:
S = 'station #17 reports temperature 23.8'
S = 'station #17 reports temperature 23.8'
values = sscanf(S, 'station #%f reports temperature %f')
values = 2×1
17.0000 23.8000
Thankyou for your answer,
Would this work even though lat and long are on the same line and are different at every instance?
Also, for example, would this only work on lines where #17 and 23.8 are?
I'm basically trying to achieve;
Long Lat Ele Time
values values values values
in a cell array or matrix.
see cells attached for an example of what im trying to achieve with fscanf (minus the empty cells)
Thanks!

Sign in to comment.

More Answers (0)

Products

Release

R2022a

Asked:

on 22 Oct 2022

Commented:

on 22 Oct 2022

Community Treasure Hunt

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

Start Hunting!