Possible Duplicate:
convert string to number array in matlab
I am a new Matlab user. I would like to know how to perform the above. I am completely stumped.
Your time and help is greatly appreciated, thanks in advance.
A string in MatLab is just an array of characters.
You can subtract '0'
to leave the value of each digit.
> '321' - '0'
ans =
3 2 1
Or, the less cryptic str2num
or str2double
applied to each element of the character array
arrayfun(@str2double, '321')
As a bonus, this will also return NaN
for string values corresponding to non-scalars, i.e.
>> arrayfun(@str2double, '321a')
ans =
3 2 1 NaN
Thus, for string '321a4'
the following returns only the valid scalars:
b = arrayfun(@str2double, '321a4')
c = b(~isnan(b))
c =
3 2 1 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With