MATLAB Answers

sscanf not working for text on multiple lines.

3 views (last 30 days)
Hannah Taylor
Hannah Taylor on 31 Mar 2020
Commented: Guillaume on 31 Mar 2020
I'm trying to read text from a file and put it into various collums. It works when I use the following txt and script:
My txt file looks like:
{co-ordinates 1.5 2.5 4.8 weighting 11.713}{co-ordinates 2.5 2.8 1.7 weighting 21.4}{co-ordinates 1.5 2.5 4.8 weighting 11.7}
My code looks like:
fopen practice2.txt
str = fileread('practice2.txt')
mat = sscanf(str,'{co-ordinates%f%f%f weighting%f}',[4,Inf]).'
mat =
1.5000 2.5000 4.8000 11.7130
2.5000 2.8000 1.7000 21.4000
1.5000 2.5000 4.8000 11.7000
Now im trying to use a different txt file that looks like this:
{
co-ordinates 102.10 93.30 128.50
weighting 1.000000}
{
co-ordinates 99.60 98.50 124.00
weighting 0.622901
}
and now all of a sudden the code doesn't work at all!? Any idea why it's no longer working and how to fix it? Thanks!

  2 Comments

Guillaume
Guillaume on 31 Mar 2020
Can you attach to your question an example file
Hannah Taylor
Hannah Taylor on 31 Mar 2020
I've uploaded the text file! It has a different file name than in the code but i'm aware of this.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 31 Mar 2020
"now all of a sudden the code doesn't work at all!"
Well, yes that would be because the text portion in your sample file is not 'co-ordinates' and 'weighting' but 'position' and 'weight', so of course when sscanf looks for 'co-ordinates' it can't fail it and aborts. You could change your sscanf format to reflect the actual text of the file:
mat = sscanf(str,' { position%f%f%f weight%f}'), [4, Inf]).'
or just tell sscanf to ignore the text whatever it is, so it works in both cases:
mat = sscanf(str, ' {%*s%f%f%f%*s%f}', [4, Inf]).'

  4 Comments

Show 1 older comment
Hannah Taylor
Hannah Taylor on 31 Mar 2020
fopen practice2new.txt
str = fileread('practice2new.txt')
mat = sscanf(str,'{ position%f%f%f weight%f}',[4,Inf]).'
this is what i'm using and it gives nothing.
Hannah Taylor
Hannah Taylor on 31 Mar 2020
Actually, your second option seems to have worked!! I wonder why it wasn't working when using 'position' and 'weight'... thanks :)
Guillaume
Guillaume on 31 Mar 2020
Oh I forgot to say, the line:
fopen practice2new.txt
is completely unnecessary. For a start, that's not how you use fopen and this will leave the file open so may prevent other programs from accessing it. In any case, fileread opens the file on its own.
sscanf has so weird logic which always baffles me. With the first syntax you do need to be careful about the spaces around the text. With the %*s, the spaces are not required.
Note that you could always remove all the extra whitespace in your file with:
str = fileread('practice2.txt');
str = regexprep(str, '\s+', ' '); %replace all sequences of whitespaces (tab, spaces, line ends, etc) by just one space
but that's not really required for sscanf.

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by