MATLAB: Error converting chars to string

converterrorMATLABstringstringsstruct

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!

Best Answer

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