MATLAB Answers


convert a string vector to a vector of numbers

Asked by googo
on 16 Apr 2013


suppose I have a vector of string [1 2 3 4] and i want to change it to a vector of nmbers to do a numeric caculations, How can I do it? Thank's!


Are the '[' and ']' in the string? Are the numbers only integers or possibly floating point ?

string=['1' '2' '3' '4'] and I want to change it to : a = [1 2 3 4]

Log in to comment.


No tags are associated with this question.

1 Answer

Answer by Friedrich
on 16 Apr 2013
Edited by Friedrich
on 16 Apr 2013


as long there are no [] in the string use textscan:

textscan('1 2 3 4 6.3','%f','delimiter',' ')

In the case there are use strrep to remove the [ or ] before calling textscan.


Walter, what do you mean? I didn't understand.

You have


the destination t(i,n+1) is a single character. The number 10 and upwards require two (or more) characters to represent as a string. Therefore if c reached 10 (occurrences) the code would fail. This suggests that you have a code design problem.

Thank you... I'll try to find a way to fix it..

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Win prizes and improve your MATLAB skills

Play today