Cell array formatting /restructuring
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Image Analyst
am 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.
Akzeptierte Antwort
Chunru
am 27 Jul. 2021
Bearbeitet: Chunru
am 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)];
end
t
3 Kommentare
Weitere Antworten (1)
Dave B
am 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)
[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 Kommentare
Dave B
am 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:
Data(~cellfun(@(x)x(1)<'A',Data(:,1)),1)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!