Search code examples
arraysmatlabdatasetcellcell-array

create an new cell array everytime a loop cycles through in matlab


I am new to MatLab but I have some experience with C#. I have a large dataset <169360x97> that I need to break up into 464 cell arrays. I currently have a loop that will cycle through the dataset and make a cell array, but I can not figure out how to have the loop create a new cell array every time instead of just rewriting over the same data. Here is the loop I have written.

    b=5476;
    e=5840;
    while(b<169360)
     dataset2cell(JeaAddressKwh(b:e,1:97));
     b=e+1;
     e=e+365;
    end

I have tried the following, but i get an error message every time:

   n=16;
   b=5476;
   e=5840;
   while(b<169360)
    n=dataset2cell(JeaAddressKwh(b:e,1:97));
    n+1;
    b=e+1;
    e=e+365;
   end

So basically what I am trying to get as an output is a different cell array called 16 through 464. I would appreciate any help. Thanks.


Solution

  • In the first loop you are not saving the cell array and in the second loop you over overwriting the previous cell array and trying to add 1 to it, without saving the result.

    Try something like this:

       n=cell(16,1);
       b=5476;
       e=5840;
       i = 1;
       while(b<169360)
           n{i}=dataset2cell(JeaAddressKwh(b:e,1:97));
           i = i+1;
           b=e+1;
           e=e+365;
       end