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

5 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

kei hin
kei hin am 5 Jun. 2023
Bearbeitet: kei hin am 5 Jun. 2023
textscan(strrep(a,' ',''),'%s%f%f%f%f%f%s','Delimiter',' ');
or
textscan(strrep(a,' ',' '),'%s%f%f%f%f%f%s','Delimiter',' ');
  1 Kommentar
Andre
Andre am 5 Jun. 2023
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 5 Jun. 2023
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}
celldisp(Result)
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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by