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

on 22 May 2019
Latest activity Answered by Peter Perkins

### Peter Perkins (view profile)

on 22 May 2019
Accepted Answer by Luuk van Oosten

### Luuk van Oosten (view profile)

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');
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.

### Tags ### Luuk van Oosten (view profile)

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.

#### 1 Comment

on 22 May 2019
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....