Textscan issue with txt file data delimited space : or .

1 回表示 (過去 30 日間)
Mafalda Couto
Mafalda Couto 2017 年 9 月 5 日
コメント済み: Cam Salzberger 2017 年 9 月 5 日
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

回答 (1 件)

Cam Salzberger
Cam Salzberger 2017 年 9 月 5 日
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 件のコメント
Mafalda Couto
Mafalda Couto 2017 年 9 月 5 日
Hi Cam
The first option didn't work either. I think I'll go for the second!
Thank you very much*
Cam Salzberger
Cam Salzberger 2017 年 9 月 5 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by