MATLAB Answers

error converting chars to string

54 views (last 30 days)
Teshan Rezel
Teshan Rezel on 20 Jan 2021
Edited: dpb on 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 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 20 Jan 2021
Edited: Adam Danz on 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 Comments

Show 1 older comment
dpb
dpb on 20 Jan 2021
That makes it run, but why are you embedding a string variable into a cell?
>> Samples{1}=convertCharsToStrings(regexprep({'Freddy Mae'},'\s+',''))
Samples =
1×1 cell array
{["FreddyMae"]}
>>
Just use the array of strings or don't convert the output to a string if going to use cell strings.
Adam Danz
Adam Danz on 20 Jan 2021
Thanks, dpb, good catch. The string class has been out for 4+ years and I still mainly think in chars 🙄.
dpb
dpb on 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.

Sign in to comment.

More Answers (1)

dpb
dpb on 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 Comments

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!