How to store data to a new column after each iteration.
Show older comments
Hi,
I am looking at some data and each loop I define am sample from this data and the want to store it in a table.
So each time loop I want to add a new column to a table and store the data (so the table would ideally be (x1(1), x2(1), x1(2), x2(2)...etc) , however the length of this data is different each time and that seems to make it more difficult.
Am wondering if theres any obvious way to do this, or just to store it directly into a excel file in this format.
thank you in advance.
m=[];
for x=1:9
FileName= sprintf('A_S_%d.txt', x); %read file
data = importdata(FileName);
t=data(:,1);
d=data(:,2);
Max_Depth= max(d);
[maxvaly,idx] = max(d) ;
Time = t(idx) ;
[val,idx2]=min(abs(d));
minVal=d(idx2);
Time2 = t(idx2) ;
tableA(x,:)=[{x} Time2 minVal Time Max_Depth];
t3 = array2table(tableA, 'VariableNames', {'Repeat','Time_of_Impact_s','Impact_Depth_nm', 'Time_of_Max_Depth_s', 'Max_Depth_nm'});
x1=t([idx2:idx]);
x2=d([idx2:idx]);
% m=[m; t([idx2:idx])];
% m=[m; d([idx2:idx])];
m=[m;x1;x2;]
plot(x1,x2); hold on
title ('Impact Depth From 0 to Max'), xlabel('Time(s)'), ylabel('Impact Depth (nm)')
end
1 Comment
Anirudh Singh
on 14 Jul 2020
These are some duscussion over same issue, you can try those if these can work for you:
- https://www.mathworks.com/matlabcentral/answers/411566-how-can-i-write-data-to-excel-sheet-in-the-next-column-automatically-when-my-program-runs-for-the-2n
- https://www.mathworks.com/matlabcentral/answers/127895-create-new-column-for-data-after-each-iteration-of-a-for-loop
- https://www.mathworks.com/matlabcentral/answers/34460-xlswrite-to-write-in-a-different-column-in-each-iteration
- https://www.mathworks.com/matlabcentral/answers/156190-dynamically-change-column-in-excel-worksheet-inside-a-for-loop
Answers (0)
Categories
Find more on Loops and Conditional Statements 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!