textscan does not scan the text as accurate as strread, i have "errors"
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(2 个)
Harsh Saxena
2023-6-5
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
2023-6-5
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!