Cell array formatting /restructuring
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tomaszzz
el 27 de Jul. de 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 comentario
Image Analyst
el 27 de Jul. de 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.
Respuesta aceptada
Chunru
el 27 de Jul. de 2021
Editada: Chunru
el 28 de Jul. de 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
3 comentarios
Más respuestas (1)
Dave B
el 27 de Jul. de 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 comentarios
Dave B
el 27 de Jul. de 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)
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!