user1612925
user1612925

Reputation: 51

convert string to number array in matlab

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?

Upvotes: 5

Views: 11110

Answers (3)

Georgggg
Georgggg

Reputation: 564

You can use cellstr:

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

Upvotes: 0

Zakael
Zakael

Reputation: 1

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

Upvotes: 0

Eastsun
Eastsun

Reputation: 18859

str = '123';
num = str - '0';
% num = [1 2 3];

Upvotes: 25

Related Questions