How can I extract data from a table and put it into a new one?
1 view (last 30 days)
Show older comments
Tessa Coenraad
on 22 May 2019
Answered: Peter Perkins
on 22 May 2019
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 Comments
Accepted Answer
Luuk van Oosten
on 22 May 2019
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.
More Answers (1)
Peter Perkins
on 22 May 2019
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!