Textscan issue with txt file data delimited space : or .

1 view (last 30 days)
Mafalda Couto
Mafalda Couto on 5 Sep 2017
Commented: Cam Salzberger on 5 Sep 2017
Hello I am using textscan to export data from txt file:
'17:12:35 -1 -1 1 12 -1 -10.017 35 comment'
I would like to separate in each data value (hours min sec # # # # # #.# # string)
I used data = [textscan(fopen(strcat(PathName, FileName)),'%f:%f:%f %f %f %f %f %f %f.%f %f %s','Delimiter','\t','HeaderLines', 17)];
It works fine to distinguish time in hours min and sec, however I don't know how to delimit when the dot occurs. I have 2 different value informations. One before and one after the dot that I need to separate.
Any help?
Thank you

Answers (1)

Cam Salzberger
Cam Salzberger on 5 Sep 2017
Hello Mafalda,
%f will specify to interpret the text as a floating point number. Since floating point numbers can have decimals, it accepts the period as part of the number.
I can think of two options here:
1) Assuming you'll never have an entry like "3.1414.27", you can try accepting both the numbers around the period as integers (%d or %i) instead of floating point. Then convert back to double afterwards.
2) Accept it as a single double, and split it into two entities afterwards (using mod and round or something along those lines).
-Cam
  2 Comments
Cam Salzberger
Cam Salzberger on 5 Sep 2017
Hmm, when I do a simple parse, it seems to work for the integer datatypes.
teststr = '17:12:35 -1 -1 1 12 -1 -10.017 35 comment';
sscanf(teststr,'%f:%f:%f\t%f\t%f\t%f\t%f\t%f\t%d.%d\t%f\t%s')
Maybe it's something specific about textscan's arguments? Glad the second option is working for you though.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!