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
댓글 수: 0
채택된 답변
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개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!