Possible Duplicate:
convert string to number array in matlab
Is there a simple way in Matlab to convert a string like this
'123456789'
into a vector like this ?
[1 2 3 4 5 6 7 8 9]
Introduction to Cell to String MATLAB. There are two commands used to covet cell data into string format one is char and the other is a string. char and string commands extract all the data from cell arrays and stored in the form of string. In Matlab, we use string notations as data in single or double quotes ( “ ” or ‘ ‘ ).
The MATLAB function str2double () accepts a limited number of characters representing values in the input string, one of which is the decimal point. In this example, we will look at how you can convert strings representing numbers with a decimal point to a double-precision scalar.
Input array, specified as a numeric matrix. int2str returns character arrays only. Starting in R2016b, you can convert numeric arrays to string arrays using the string function. Generate C and C++ code using MATLAB® Coder™. Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.
To convert a number to a string that represents it, use the string function. The string function converts a numeric array to a string array having the same size. You can specify the format of the output text using the compose function, which accepts format specifiers for precision, field width, and exponential notation.
If all you have is contiguous characters from 0 to 9:
v = double(s)-'0';
double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.
one way would be using regexp
for this. But of course it only works for single digit numbers.
>> str = '123456789';
>> num = regexp(str,'\d')
num =
1 2 3 4 5 6 7 8 9
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