textscan does not scan the text as accurate as strread, i have "errors"

1 次查看(过去 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.


kei hin
kei hin 2023-6-5
编辑:kei hin 2023-6-5
textscan(strrep(a,' ',''),'%s%f%f%f%f%f%s','Delimiter',' ');
textscan(strrep(a,' ',' '),'%s%f%f%f%f%f%s','Delimiter',' ');
  1 个评论
Andre 2023-6-5
so the issue was the different spacing then? the soluton to replace the longer spacingn with one like the others. ok, wasnt aware of that function. thanks man :D


更多回答(2 个)

Harsh Saxena
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!
  1 个评论
Andre 2023-6-5
oh, ok thats a good explaination, thanks. i have 2 answers here slving the problem, sadly i can only mark one as an answer


Walter Roberson
Walter Roberson 2023-6-5
a = "C +01.314 +027.11 +01.225 +01.578 +01.579 Air"
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)
Result = 1×7 cell array
{1×1 cell} {[1.3140]} {[27.1100]} {[1.2250]} {[1.5780]} {[1.5790]} {1×1 cell}
Result{1}{1} = C Result{2} = 1.3140 Result{3} = 27.1100 Result{4} = 1.2250 Result{5} = 1.5780 Result{6} = 1.5790 Result{7}{1} = Air


Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息




Community Treasure Hunt

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

Start Hunting!

Translated by