Matlab character to string convertion problem. What function to use?
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 c开发者_StackOverflowlub 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?
Simple solution is to use sscanf
:
x =' 1234 56789 7654'
sscanf(x, '%d')
ans =
1234
56789
7654
There are several ways of performing what you want. One of them is strtok.
x = '1234 56789 7654';
[fst rest] = strtok(x,' ');
STR2NUM works well for this task:
>> x = '1234 56789 7654'; >> x = str2num(x)' x = 1234 56789 7654
Just to add another answer to the mix...
y = textscan(x, '%d %d %d')
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);
精彩评论