How to change textscan to fscanf?
5 views (last 30 days)
Show older comments
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
1 Comment
Walter Roberson
on 22 Oct 2022
Note: @Sarah attached a sample file at https://www.mathworks.com/matlabcentral/answers/1819315-how-to-import-a-gpx-file-without-using-gpxread#comment_2403540
Accepted Answer
Walter Roberson
on 22 Oct 2022
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
Walter Roberson
on 22 Oct 2022
Hint:
S = 'station #17 reports temperature 23.8'
values = sscanf(S, 'station #%f reports temperature %f')
More Answers (0)
See Also
Categories
Find more on Logical 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!