error converting chars to string
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Teshan Rezel
am 20 Jan. 2021
Bearbeitet: dpb
am 20 Jan. 2021
Hi folks,
I keep getting an error when running the following line of code:
Names = xml2struct([TestFolder '\captureSettings.xml']);
NumbOfSamples = str2double(Names.Children(20).Children.Data);
for m = 1:NumbOfSamples
Name = Names.Children(22).Children(2*m).Children(4).Children.Data;
Samples(m) = convertCharsToStrings(regexprep(Name, '\s+', ''));
end
The error is:
The following error occurred converting from string to cell:
Conversion to cell from string is not possible.
I can't seem to find why this error appears.Any thoughts on this would be much appreciated!
Thanks in advance!
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 20 Jan. 2021
Bearbeitet: Adam Danz
am 20 Jan. 2021
Assuming Samples is a cell array
Samples{m} = convertCharsToStrings(regexprep(Name, '\s+', ''));
% ^ ^
If Samples isn't predefined, don't forget to preallocate it:
Samples = cell(1,NumbOfSamples);
for m = 1:NumbOfSamples
...
end
As dpb mentioned, if you're using strings instead of char vectors,
Samples = strings(1,NumbOfSamples);
% ^^^^^^^
for m = 1:NumbOfSamples
Name = Names.Children(22).Children(2*m).Children(4).Children.Data;
Samples(m) = convertCharsToStrings(regexprep(Name, '\s+', ''));
% ^ ^
end
4 Kommentare
Adam Danz
am 20 Jan. 2021
Thanks, dpb, good catch. The string class has been out for 4+ years and I still mainly think in chars 🙄.
dpb
am 20 Jan. 2021
Bearbeitet: dpb
am 20 Jan. 2021
strings have their place, but they're also limited in their syntax.
One of my pet peeves is the only way to get a substring is to cast back to char() by cell curlies dereferencing notation...
For the above example
>> Samples(1)=convertCharsToStrings(regexprep({'Freddy Mae'},'\s+',''))
Samples =
"FreddyMae"
>> Samples(1:2)
Index exceeds array bounds.
>> Samples{1:2}
Index exceeds array bounds.
>> Samples{1}(1:2)
ans =
'Fr'
>>
but then it returns a char() string instead of the string variable class which you then have to either convert or remember to use character indexing unless you directly store the result into a string variable; then the typecast does occur on assignment. But, temporaries aren't, so you can end up with syntax errors to fix.
And there's no substring() function to abstract to higher level. Seems to be a new class that is let into the wild before being fully developed.
Weitere Antworten (1)
dpb
am 20 Jan. 2021
You've previously defined Samples as a cell array (either deliberately or by accident) --
>> Samples={}
Samples =
0×0 empty cell array
>> Samples(1)=convertCharsToStrings(regexprep({'Freddy Mae'},'\s+',''))
The following error occurred converting from string to cell:
Conversion to cell from string is not possible.
>>
Further examination reveals:
>> Samples=[]
Samples =
[]
>> Samples(1)=convertCharsToStrings(regexprep({'Freddy Mae'},'\s+',''))
Samples =
NaN
>> Samples=string("")
Samples =
""
>> Samples(1)=convertCharsToStrings(regexprep({'Freddy Mae'},'\s+',''))
Samples =
"FreddyMae"
>>
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!