error converting chars to string
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Teshan Rezel
el 20 de En. de 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 comentarios
Respuesta aceptada
Adam Danz
el 20 de En. de 2021
Editada: Adam Danz
el 20 de En. de 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 comentarios
Adam Danz
el 20 de En. de 2021
Thanks, dpb, good catch. The string class has been out for 4+ years and I still mainly think in chars 🙄.
dpb
el 20 de En. de 2021
Editada: dpb
el 20 de En. de 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.
Más respuestas (1)
dpb
el 20 de En. de 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 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!