5

I have a script in which a string of number is entered

string='123'

or

string='9823'

I am trying to convert this into an array of the form [a,b,c,d] e.g from a string of '123' to a numerical array [1,2,3]

Any tips on how to do this?

1
  • 6
    OP: Please do not replace your questions with nonsense. Commented Aug 20, 2012 at 23:44

3 Answers 3

25
str = '123';
num = str - '0';
% num = [1 2 3];
Sign up to request clarification or add additional context in comments.

Comments

0

use the function str2num()

str = '123';
str = str2num(str);

Note: To verify that i'm correct, type in 'whos str' in the command window, and check the class. A string has a class, char, and numeric values have a class, double

1 Comment

Your code produces a 1x1 double, not a 3x1 array of doubles as required.
0

You can use cellstr:

cellstr('123')
ans =
{
  [1,1] = 123
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.