1

I have cell array which has strings with different length:

rr ={'1 2 5';
     '5 6 1';
     '12 56 2';
     '12 1';
     '343 2 -5 1 5';
     '1  5  3  2 0'}

I want to make different matrices of numbers and not string based on the strings length:

a1 = [1 2 5; 5 6 1; 12 56 2]
a2 = [12 1]
a3 = [343 2 -5 1 5; 1  5  3  2 0]

I have a large set and my actual data will have many matrices like a1,a2,a3.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
EkEhsaas
  • 93
  • 9
  • 2
    @EBH when editing a question, please also take care to remove unnecessary stuff, like the "please help me", don't forget the title and if useful, add relevant tags. – Adriaan Oct 06 '16 at 11:06
  • 2
    Don't create these matrices, please. You'll end up using horrible functions which both obfuscate and slow down your code dramatically. Leave it as is, a cell array is perfectly fine and indexable. Why would you even want so many variables? You can read up on why this is a bad idea in [this answer of mine](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170), especially the references contained therein. – Adriaan Oct 06 '16 at 11:11
  • I wana use numbers. i tried to use str2num but it gives error for my rr cell – EkEhsaas Oct 06 '16 at 11:14
  • That doesn't answer the question as to *why* you want so many variables. You can perfectly well store numbers in cells as well. – Adriaan Oct 06 '16 at 11:14
  • 2
    I haven't tested but: `cellfun(@(c) str2num(c), rr,'UniformOutput', false)`. – Stewie Griffin Oct 06 '16 at 11:15
  • I actualy want to do operations depending on num of columns. stewie if separate variables r not good. can i combine the ones with same columns in one cell like rr{1}= [1 2 5; 5 6 1; 12 56 2] – EkEhsaas Oct 06 '16 at 11:19
  • @Adriaan, OP might want something like `a{1} = [1 2 5; 5 6 1; 12 56 2]; a{2} = [12 1]; ...`. I don't have the time myself at the moment... – Stewie Griffin Oct 06 '16 at 11:20
  • yes stewie i want that ! but if then again three columns come after a{3}=[343 2 -5 1 5; 1 5 3 2 0] then I want them in a{4} and not in a{1}. – EkEhsaas Oct 06 '16 at 11:24
  • @StewieGriffin yea, that's what I'd do. You'd need some fancy `cellfun` with `strsplit` and then store stuff in matrices, but you need to know which size those matrices need and whether you can preallocate. That actually makes this rather difficult. – Adriaan Oct 06 '16 at 11:24

1 Answers1

2

Here is one way:

rr ={'1 2 5';
     '5 6 1';
     '12 56 2';
     '12 1';
     '343 2 -5 1 5';
     '1  5  3  2 0'}

%// convert 
x = cellfun(@(x) sscanf(x,'%f'), rr,'uni',0)
%// count
n = cellfun(@numel,x)
%// distribute
s = accumarray(n,1:numel(n),[],@(ii) {x(ii)} )
%// remove empty elements
s = s(~cellfun('isempty',s)) 
%// assign
m = cellfun(@(y) [y{:}].' ,s, 'uni',0)

%// last step I'd try to avoid
[a,b,c] = m{:};

a =

    12     1


b =

    12    56     2
     5     6     1
     1     2     5


c =

     1     5     3     2     0
   343     2    -5     1     5

I'd recommend you to avoid the last step and keep working the cell array m, as you don't know in advance how many a1,a2 ... and so on you need. Structs are also a good option.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • @EkEhsaas no it's not possible with this approach, and you should definetely rethink your approach of using multiple variable names – Robert Seifert Oct 06 '16 at 16:55