Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab, how to convert a string of integers into a vector? [duplicate]

Tags:

matlab

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]
like image 664
carlito Avatar asked Nov 03 '12 23:11

carlito


People also ask

How do I convert a cell to a string in MATLAB?

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 ‘ ‘ ).

How do I convert a decimal point to a scalar in MATLAB?

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.

How do I convert a numeric array to a string array?

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.

How do you convert a number to a string in Python?

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.


2 Answers

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.

like image 155
pedrosorio Avatar answered Sep 24 '22 14:09

pedrosorio


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
like image 38
Not Bo Styf Avatar answered Sep 21 '22 14:09

Not Bo Styf