Cell array formatting /restructuring

4 visualizzazioni (ultimi 30 giorni)
Tomaszzz
Tomaszzz il 27 Lug 2021
Modificato: Chunru il 28 Lug 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 Commento
Image Analyst
Image Analyst il 27 Lug 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.

Accedi per commentare.

Risposta accettata

Chunru
Chunru il 27 Lug 2021
Modificato: Chunru il 28 Lug 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)];
end
t
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
  3 Commenti
Tomaszzz
Tomaszzz il 28 Lug 2021
Hi @Chunru. Thanks for your help with this. I have now tried to expand the code by trying to extract more data, for example 'Average pressure' as in the example below (in italics). However errors occur whcih i do not understand (bolded at the bottom). . Could you please help?
FRAME varibale occurs once whereas SENSOR varibale occurs twice so I thought it should not matter if AVERAGE PRESSURE variable occurs multiple times between dx_frame_start(i):idx_frame_end(i)
for i=1:length(idx_frame_start)
a.FRAME = str2double(d.data_raw{idx_frame_start(i), 2});
idx = find(strcmp(d.data_raw(idx_frame_start(i):idx_frame_end(i), 1), 'Time'));
a.TIME = d.data_raw{idx_frame_start(i)-1+idx, 2};
idx = find(strcmp(d.data_raw(idx_frame_start(i):idx_frame_end(i), 1), 'SENSOR'));
a.SENSOR = d.data_raw{idx_frame_start(i)-1+idx, 2};
idx = find(strcmp(d.data_raw(idx_frame_start(i):idx_frame_end(i), 1), 'Average Pressure (kPa)'));
a.AVR_PRESSURE = d.data_raw{idx_frame_start(i)-1+idx, 2};
t = [t; struct2table(a)];
end
All tables being vertically concatenated must have the same number of variables.
Chunru
Chunru il 28 Lug 2021
Modificato: Chunru il 28 Lug 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.

Accedi per commentare.

Più risposte (1)

Dave B
Dave B il 27 Lug 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)
[hdr,~,ind]=unique(data(:,1));
% 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);
end
%%%%%%
% BONUS 1: is everything numeric? Do we want cells containing doubles
% instead of chars?
out(2:end,:)=cellfun(@double,out(2:end,:),'UniformOutput',false);
% 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,:))
  2 Commenti
Tomaszzz
Tomaszzz il 27 Lug 2021
Hi Dave,
Many thanks for your time in working this out.
Re: 'there are several rows that begin with a number': I want to ignore them and just focus on rows that starts with a text and the subsequent numeric values
Re the code: so it gives me a table with a structure that I ultimately wanted. Due to my lmited Matlab knowledge, I was not sure how can a link/import the actual data into it, which the code from Chunru already does.
Dave B
Dave B il 27 Lug 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:
Data(~cellfun(@(x)x(1)<'A',Data(:,1)),1)

Accedi per commentare.

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by