MATLAB Answers

Cell array formatting /restructuring

조회 수: 3(최근 30일)
eyko
eyko 2021년 7월 27일
편집: Chunru 2021년 7월 28일
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
Image Analyst
Image Analyst 2021년 7월 27일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Chunru
Chunru 2021년 7월 27일
편집: Chunru 2021년 7월 28일
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
Chunru
Chunru 2021년 7월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Dave B
Dave B 2021년 7월 27일
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
Dave B
Dave B 2021년 7월 27일
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)

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by