Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to create cell array for time format elements in matlab

Asked by saharsahar on 28 Jun 2013

Hi all, I want to create a cell array and increasing the rows at each loop, while the elements of my cell array are all in time format , for example 02:34:12.

Following is my code.

uuniq=unique(rad_index);
for i=1:length(uniq)
      for j=1:length(rad_index)
          if uniq(i)==rad_index(j)
              dis(j,:)= diff(j,:);
          end
     end
  MyArray{i}=dis;
end

where value of dis at i=1 and end of second loop will be:

dis=

00:02:11

00:03:47

00:03:46

now I want to create a cell array which keep these information as the first row and increase the row when i=2, i=3, .... I was thinking MyArray in above code should give me what I want but actually gives me the following error:

Cell contents assignment to a non-cell array object.

any idea is appreciated ...

0 Comments

saharsahar

Products

No products are associated with this question.

1 Answer

Answer by Muthu Annamalai on 28 Jun 2013
Accepted answer

Your error message means that you are indexing the cell-array to get a subset of its elements as another cell-array.

i.e.

>> y = {1,2,3}; 
>> z = y(2); class(z) %z is a cell-array of 1 element
>> z = y{2}; class(z) %z is a double

so you want to change your code to use '{}' braces to access the underlying element, and use '()' paranthesis to access sub-cell-array.

HTH

4 Comments

saharsahar on 28 Jun 2013

well, it seems error is removed but I am not able to access the data . I changed dis(j,:)= diff(j,:); to dis{j,:}= diff(j,:); now "dis" is a 3 by 1 cell array consist of

    '00:02:11'
    '00:03:47'
    '00:03:46'

and MyArray is 1 by 1 cell array and MyArray (1,1) is the same as Myarray consist of

         '00:02:11'
          '00:03:47'
          '00:03:46'

how I can access to elements now?

thanks

saharsahar on 28 Jun 2013

ok let me ask my question in a simple way : how we can assign a cell array as first row of another cell array and how we can acces the data?

Thanks

Muthu Annamalai on 1 Jul 2013
>> z = {1,2,3}
z = 
      [1]    [2]    [3]
>> z{1}
ans =
       1
>> z{1} = z
z = 
      {1x3 cell}    [2]    [3]
>> z{1}{1}
ans =
       1
>> z{1}{2}
ans =
Muthu Annamalai

Contact us