Save output from for loop without overwriting previous outputs?
25 views (last 30 days)
Show older comments
Let't say I want to save the output "Qv_maalt" of each iteration to a vector called "Final". Currently I have 10 files for the loop to process, and thus, 10 iterations.
Where do i put the for loop in order to avoid overwriting of the variable "Qv_maalt" at each iteration, and instead write the output of each iteration to the same vector, and thus ending up with a vector of 10 different values.
My current code reads:
close all
files = dir('*.mat');
for file = files'
load([file.name]);
dt = Data1_time_M_airsupply(end);
hold on
midling = ones(1, 1000)/1000;
filtrering = filter(midling,1,Data1_M_airsupply_IIR_Filter);
plot(filtrering)
deltaMass = filtrering(1) - filtrering(end);
Qm = deltaMass / dt;
T = mean(Data1_T_orifice);
tempVektor = [35 30 25 20 15 10 5 0 -5 -10 -15 -20 -25];
densVektor = [1.145 1.164 1.183 1.204 1.225 1.246 1.269 1.292 1.316 1.341 1.367 1.394 1.422];
Rho = interp1(tempVektor, densVektor, T);
Qv_maalt = Qm / Rho;
end
Apologies for lack of brevity.
Thanks in advance!
0 Comments
Accepted Answer
Geoff Hayes
on 21 Mar 2015
Peter - just define the vector Final (you may want to come up with a more descriptive name for it) before entering the for loop and update it at each iteration. Something like
files = dir('*.mat');
Final = zeros(length(files),1);
for k=1:length(files)
load([files(k).name]);
% etc.
Qv_maalt = Qm / Rho;
Final(k) = Qv_maalt;
end
The above assumes that Qv_maalt is a scalar. If it is a vector, then you will need to size Final accordingly. If Qv_maalt is of a different size on each iteration of the loop, then consider using a cell array for Final.
14 Comments
Geoff Hayes
on 19 Aug 2020
Turbulence Analysis answer moved here
Yes, I have tried that got a attached mat file, here as you can see I have got 13x4 matrix for 10 files, actually row 3 and 4 belongs to same input file B00003.. Is there any possibility to group this...
More Answers (0)
See Also
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!