error converting chars to string

조회 수: 7 (최근 30일)
Teshan Rezel
Teshan Rezel 2021년 1월 20일
편집: dpb 2021년 1월 20일
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!

채택된 답변

Adam Danz
Adam Danz 2021년 1월 20일
편집: Adam Danz 2021년 1월 20일
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
Adam Danz
Adam Danz 2021년 1월 20일
Thanks, dpb, good catch. The string class has been out for 4+ years and I still mainly think in chars 🙄.
dpb
dpb 2021년 1월 20일
편집: dpb 2021년 1월 20일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2021년 1월 20일
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"
>>

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by