How can I extract data from a table and put it into a new one?

2 次查看(过去 30 天)
Hi!
We made a loop to create a table where all our measurements can be put in. The problem is, that all the measurements are done 4 times(for 1 machine), and we want a new table with the four measurements (from one machine) put in seperate colums.
Our loop looks like this:
files=dir('*.log');
M=NaN(length(files),1);
Lek=M;
B=M;
C=M;
R=M;
PS=M;
TV=M;
For i=1:length(files)
out=textscan(files(i).name,'M%f_Lek%f_B%f_C%f_R%f_PS%f');
A = readcell(files(i).name); %data inladen
Z = string(A);
time = Z(7:end,1); %van time een vector maken
time = strrep(time, ',', '.'); %komma's in vector in punten veranderen
time = str2double(time);
flow = Z(7:end,2);
flow= strrep(flow, ',', '.');
flow = str2double(flow);
flow = flow./60;
flowpos = flow;
flow(flow<0)= 0;
RR=23;
volpos = trapz(time, flow);
TV(i)= (volpos./RR);
TV(i)= TV(i).*1000;
M(i)=out{1};
Lek(i)=out{2};
B(i)=out{3};
C(i)=out{4};
R(i)=out{5};
PS(i)=out{6};
end
T=table(M,Lek,B,C,R,PS,TV,'VariableNames',{'M','Lek','B','C','R','PS','TV'})
If M is 1, we want TV to be in column '1' of the new table, if M is 2, we want TV to be in column '2' of the new table, etc....
How can we do this?
We managed to create a new table just like above, but he wont fill in the TV's.
Thanks in advance, Tessa

采纳的回答

Luuk van Oosten
Luuk van Oosten 2019-5-22
Beste Tessa,
You almost wrote it yourself already, if I understand your problem correctly....? You already wrote in pseudocode a perfect example of an elseif statement when you said:
"If M is 1, we want TV to be in column '1' of the new table, if M is 2, we want TV to be in column '2' of the new table, etc...."
% so you have some kind of M, lets say M is 2
M = 2
if M==1
yourcodeforputtingitincolumn1ofthetable
elseif M ==2
yourcodeforputtingitincolumn2ofthetable
elseif M ==3
yourcodeforputtingitincolumn3ofthetable
elseif M == N
yourcodeforputtingitincolumnNofthetable
else
disp('back to the drawing board')
end
If this is not what you meant, please clarify.
  1 个评论
Tessa Coenraad
Tessa Coenraad 2019-5-22
The problem is that we dont know how to write the code for putting it into the right column of a new table. Also i'm not sure if MATLAB "knows" it has to read out of the first table T....

请先登录,再进行评论。

更多回答(1 个)

Peter Perkins
Peter Perkins 2019-5-22
With no example, it's hard to understand what you have and what you want to end up with, but your description sounds very much like you want to use unstack.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by