4
x = 1234     56789     7654

x(1) is 1, x(2) is 2 and so on... there are 5 spaces in between.. size(x) = 1 23 One row with 23 columns I've tried using num2str, strcat but I cannot club 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 I use to accomplish the above?

emil
  • 105
  • 3
  • 8

5 Answers5

4

Simple solution is to use sscanf:

x =' 1234     56789     7654'

sscanf(x, '%d')

ans =

    1234
   56789
    7654
nimrodm
  • 21,218
  • 7
  • 51
  • 54
2

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

x = '1234     56789     7654';
[fst rest] = strtok(x,' ');
carlosdc
  • 11,578
  • 3
  • 38
  • 60
0

STR2NUM works well for this task:

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

x =

        1234
       56789
        7654
b3.
  • 6,979
  • 2
  • 29
  • 47
0

Just to add another answer to the mix...

y = textscan(x, '%d     %d     %d')
John
  • 5,448
  • 3
  • 44
  • 59
  • 1
    more simply as: `y = textscan(x,'%d');`. Also you should probably add `y = y{1};` – Amro Aug 12 '11 at 01:39
0

The following creates a cell array of strings then follows it up with an application of sscanf.

b = regexp(x,'\d+','match');
y = cellfun(@(a) (sscanf(a,'%d')),b); 
Jurnell
  • 41
  • 1