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

cell arraysdeleterotatetable

Hey dear Matlab-Knowers, I'm desperate …
What I got: a cell array with 4×18622 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 18622×4 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 …

Best Answer

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.