Cell array formatting /restructuring

6 views (last 30 days)
Tomaszzz on 27 Jul 2021
Edited: Chunru on 28 Jul 2021
Hi all,
I have a 242747x 20 cell array with the following data structure. Data have been imported to matlab from a csv file.
What I want to do is to create a new cell array that will have the above string values (for example 'time', 'frame' etc) listed as colum headers (not in the seperate rows as above) with respective numeric values below. For example:
Any advice/direction how to approach this please ? Attached a smaller version mat file with the data. Thank you
  1 Comment
Image Analyst
Image Analyst on 27 Jul 2021
Why does your screenshot show only 2 columns instead of 20? If Daves answer below does not work, can you attach the cell array in a .mat file? Truncate it if the mat file is more than 5 MB.

Sign in to comment.

Accepted Answer

Chunru on 27 Jul 2021
Edited: Chunru on 28 Jul 2021
Here is a portion of code for you to further expand:
% load data
d = load('Data.mat');
n = size(d.Data, 1);
% Find the FRAME keyword
idx_frame_start = find(strcmp(d.Data(:, 1), 'FRAME'));
idx_frame_end = [idx_frame_start(2:end)-1; n];
t = table();
for i=1:length(idx_frame_start)
a.FRAME = str2double(d.Data{idx_frame_start(i), 2});
idx = find(strcmp(d.Data(idx_frame_start(i):idx_frame_end(i), 1), 'Time'));
a.TIME = d.Data{idx_frame_start(i)-1+idx, 2};
idx = find(strcmp(d.Data(idx_frame_start(i):idx_frame_end(i), 1), 'SENSOR'));
a.SENSOR = d.Data{idx_frame_start(i)-1+idx, 2};
idx = find(strcmp(d.Data(idx_frame_start(i):idx_frame_end(i), 1), 'Average Pressure (kPa)'));
a.AVR_PRESSURE = d.Data{idx_frame_start(i)-1+idx, 2};
t = [t; struct2table(a)];
t = 2×4 table
FRAME TIME SENSOR AVR_PRESSURE _____ ______________ _________________________ ____________ 1 "13:06:23.089" "HX210.11.31.M7-LF S0021" 28.66 2 "13:06:23.103" "HX210.11.31.M7-LF S0021" 28.71
Chunru on 28 Jul 2021
Edited: Chunru on 28 Jul 2021
I updated the code above. It looks ok. Try using debug and set break-point before "t=..". Check the variable a. There might be some unxepected cases in data.

Sign in to comment.

More Answers (1)

Dave B
Dave B on 27 Jul 2021
eyko - In your data file there are several rows that begin with a number, I wasn't sure how you'd interpret those.
Here's an example that I think captures your task with a reduced dataset:
% First column contains headers, second column contains 'data'
data={'Col1' '1'; 'Col2' '2'; 'Col3' '3'; ...
'Col1' '4'; 'Col2' '5'; 'Col3' '6'; ...
'Col1' '7'; 'Col2' '8'; 'Col3' '9'};
% hdr will be the unique values of data(:,1), ind will be the index of the
% corresponding values in data(:,2)
% Initialize the output
out = cell(numel(find(indb==1)) + 1, numel(hdr));
% Set the first row to be the header
out(1,:) = hdr;
% fill in the results
for i = 1:numel(hdr)
out(2:end,i) = data(ind==i,2);
% BONUS 1: is everything numeric? Do we want cells containing doubles
% instead of chars?
% Bonus: put the results in a table instead of a big cell, it's easier to
% work with
t = cell2table(out(2:end,:),'VariableNames',out(1,:))
Dave B
Dave B on 27 Jul 2021
Of course, I like Chunru's answer too, I wasn't sure if you needed something which was agnostic to the names of the header variables.
For future reference, here's a trick for grabbing the items that aren't numbers:

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!