textscan does not scan the text as accurate as strread, i have "errors"
7 views (last 30 days)
Show older comments
Hi everyone,
i have a string with several values from an alicat as input and i want to cut into several values using textscan. i have aseen a very old script, that used strread, but there is an information its not available for so long, so i write my own script now and update everything else accordingly (serial to serialport and so on). that is also my first time doing communication with a serial device.
The string is: a = "C +01.314 +027.11 +01.225 +01.578 +01.579 Air"
directly visible is that "Air" has 4 spaces, every other value just one and a sign.
textscan(a,'%s%f%f%f%f%f%s','Delimiter',' ')
that is how i tried it.
the Result is ...
in 1,1 there is only "C", despite being a 2x1 Cell.
I also cant get the "NaN" away for some reason and at the end "Air" is missing, but instead quotation marks are there.
It is working fine with strread, a little differently, but it works. I have no idea whats going on.
I tried that on 2022b and 2023a with the same results.
0 Comments
Accepted Answer
More Answers (2)
Harsh Saxena
on 5 Jun 2023
Hi Andre,
The reason this problem is occuring is due the presence of extra spaces before Air. This leads to dividing the string with blank space delimeter as follows:
[C , 1.314 , 27.11 , 1.225 , 1.578 , 1.579 , '' , '' , '' , Air]
But since the specified argument for data types is '%s%f%f%f%f%f%s'. Textscan will take the specified data types and merge the rest to form a cell until we reach the end of string like this:
{C , 1.314 , 27.11 , 1.225 , 1.578 , 1.579 , ''
'' , NaN , [] }
This explains the presence of 2x1 cell in 1,1 block, presence of NaN value in 2,2 block(since empty character is equivalent to NaN) and Air won't be present because converting 'Air' to float will result in nothing.
You can try modifying the string to remove the extra spaces before using textscan to get the correct results.
Hope this helps!
Walter Roberson
on 5 Jun 2023
a = "C +01.314 +027.11 +01.225 +01.578 +01.579 Air"
Result = textscan(a,'%s%f%f%f%f%f%s','Delimiter',' ', 'multi', true)
celldisp(Result)
See Also
Categories
Find more on Characters and Strings 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!