Scan a text file with empty ele in line
Show older comments
I have a text file whose content is like this:
I would like to use fscanf command in matlab
0.00316047 0.00189992 0.00186791 0.00170366 0.00145677 0.0020697 0.00316047 0.00287378 0.00226645 1
85.1637 44.9496 59.0738 74.457 82.4159 {} 51.9875 54.7822 85.1637 1
as can be seen, there is an empty ele in 2nd line (i.e. {}) that I want to detect while scanning the text file and recognize it's index.
Any suggestion ?
5 Comments
Walter Roberson
on 27 Jul 2018
Please do not close questions that have an answer.
Walter Roberson
on 28 Jul 2018
Does the input have a literal {} or is it column oriented and you put in {} as a marker for emphasis? If it is column oriented is it fixed width or is it tab separated?
dpb
on 28 Jul 2018
Ah! Mayhaps that was the issue. She coulda' said... :)
Walter Roberson
on 28 Jul 2018
Sample data files seldom go wrong.
Answers (1)
>> t=readtable('reza.txt','TreatAsEmpty','{}')
ans =
2×10 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10
_________ _________ _________ _________ _________ _________ _________ _________ _________ _____
0.0031605 0.0018999 0.0018679 0.0017037 0.0014568 0.0020697 0.0031605 0.0028738 0.0022665 1
85.164 44.95 59.074 74.457 82.416 NaN 51.987 54.782 85.164 1
>>
ADDENDUM
If Walter is right that it is tab-delimited and the curlies are just a showpiece, then
>> [R,cnt]=fscanf(fid,'%f',[10,inf]);
>> cnt =
15
>>
The empty field is the next after the successful 15 elements were read...
>> R.'
ans =
0.0032 0.0019 0.0019 0.0017 0.0015 0.0021 0.0032 0.0029 0.0023 1.0000
85.1637 44.9496 59.0738 74.4570 82.4159 0 0 0 0 0
>>
5 Comments
Walter Roberson
on 27 Jul 2018
Reza comments:
no
Walter Roberson
on 27 Jul 2018
Reza, what difficulty did you have with the proposed solution? Are you perhaps using a version before R2013b ?
dpb
on 27 Jul 2018
Probably because it only reads/converts and didn't see the next step to locate since the question was to find them...but did seem a little harsh. :)
Walter Roberson
on 28 Jul 2018
detectImportOptions for readtable() can handle tab delimiting and can even figure out column-oriented data that has consistent spacing.
dpb
on 28 Jul 2018
Well, OP seemed adamant on fscanf...just obliging! :)
Categories
Find more on Language Support in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!