Transforming a cell array into a table, rotating it a 90 degree and deleting particular rows

3 views (last 30 days)
Hey dear Matlab-Knowers, I'm desperate ...
What I got: a cell array with 4x18622 cells. Within the first two lines I have int variables (the ID of my object and the state of it), in the 3rd and 4th I have a date and time stamp.
What I want: I want to reorganize my data the way, that I can handle it in a table. Bc later on I have to do calculations with it. Besides this I have to filter/delete those rows, in which I have a particular number in the second line.
What I did so far: So what I basically tried - and succeeded in, was taking the cells (at least the second line, because there I'm able to find my elimination parameter) and delete each row with a '2' as value. I did this with the help of an if-loop within a while-loop ... Only until I realized, that this is only half of what I have to do - missed out to also delete the "rest of the row" part of line 1, 2 and 3 ... yay ... and I'm not sure how to handle those time variables.
What I ask you:
  • How to not only transform this array into a table (I know there exists the function 'cell2table' ..) but also turn it around so I got a 18622x4 table.
  • Perfect would be, if I could also name my rows according to my classes (ID, State, Time, Date)
I rlly appreciate every help I get!! Best regards ...

Accepted Answer

Adam Danz
Adam Danz on 12 Jul 2018
Edited: Adam Danz on 12 Jul 2018
Nicely formatted question! Here's the fake data I'm working with in the format you described.
% Fake data
data = num2cell(randi(10,[4,100]));
You mentioned, ' I have to filter/delete those rows, in which I have a particular number in the second line' which is difficult to interpret since the 2nd line is itself a row. I interpretted it as deleting columns of 'data' where there is a value '2' in the 2nd row. You can adapt this as needed and you certainly do not need a loop.
% Identify and delete columns where row2 == 2
colIdx = [data{2,:}] == 2;
data(:,colIdx) = [];
Here's how to transpose the array and store it in a table and name the columns. You mentioned naming the rows which is also possible but I think you meant columns. The tick character following 'data' transposes the array and that's how it's rotated.
% Transpose matrix and store as table
dataTable = cell2table(data', 'VariableNames', {'ID', 'State', 'Date', 'Time'});
Let me know if you need help adapting this to your needs but it should be straight forward.
  2 Comments
jrbbrt
jrbbrt on 12 Jul 2018
Wow! Especially in comparison to my loops ... what you wrote is so simple and nice :) Indeed it worked perfectly for me! Thank you so much!!
Guillaume
Guillaume on 12 Jul 2018
Note that doing the filtering is probably faster after the conversion to table. The syntax is certainly simpler (see my answer which is otherwise identical to Adam's)

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 12 Jul 2018
%note the ' below to transpose the 4xN cell array into a Nx4 cell array
t = cell2table(yourcellarray', 'VariableNames', {'ID', 'State', 'Time', 'Date'})
%delete rows of table whose state is 2:
t(t.State == 2, :) = []

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!