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 Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by