MATLAB Answers


How can I convert this table to a cell array as shown in the screenshot?

Asked by zhongjie
on 2 Sep 2019
Latest activity Commented on by zhongjie
on 6 Sep 2019
I have a CSV file that looks like thistable.jpg and I would like to convert it to two cell arrays by id.
One array contains all "t" of the same id (shown above), and the other array contains all "measure" of the same id as shown below


Sign in to comment.




2 Answers

Answer by Andrei Bobrov
on 5 Sep 2019
Edited by Andrei Bobrov
on 6 Sep 2019
 Accepted Answer

tout = varfun(@(x){x(:)'},T,'GroupingVariables','id');
C = tout{:,3:end};
measure = C(:,1);
t = C(:,2);


Show 1 older comment
Thank you! It appears to give a 2x1 cell rather than the 1x2 I was looking for. Also the elements inside the cell need to be transposed. But I can do the rest myself. Thank you very much for the answer!
I'm fix (about transpose).
Maybe accept it answer? Or ...
Sorry I'm new to this community, didn't really notice that "Accept this answer" button. My bad!

Sign in to comment.

Answer by Anmol Dhiman on 5 Sep 2019

Use the following commands
T = readtable('filename.csv')
c{1} = T{,'measure'}
c{2} = T{,'measure'}

  1 Comment

Thank you very much for the answer!

Sign in to comment.