Matlab for string conversion. Which function to use?

x = 1234 56789 7654 

x (1) is 1, x (2) is 2, etc. there are 5 spaces between them. size (x) = 1 23 One row with 23 columns I tried using num2str, strcat, but I can not combine the numbers. y = num2str (x), y = strcat (x)

I want it to be .. x (1) = 1234, x (2) = 56789, x (3) = 7654

What function should be used to accomplish the above?

+4
source share
5 answers

A simple solution is to use sscanf :

 x =' 1234 56789 7654' sscanf(x, '%d') ans = 1234 56789 7654 
+4
source

There are several ways to accomplish what you want. One of them is strtok.

 x = '1234 56789 7654'; [fst rest] = strtok(x,' '); 
+2
source

STR2NUM works well for this task:

  >> x = '1234 56789 7654';
 >> x = str2num (x) '

 x =

         1234
        56789
         7654
0
source

Just add another answer to the mix ...

 y = textscan(x, '%d %d %d') 
0
source

Next, an array of row cells is created, and then executed using the sscanf application.

 b = regexp(x,'\d+','match'); y = cellfun(@(a) (sscanf(a,'%d')),b); 
0
source

All Articles