How can I reset my variables after each iteration and then save them ?
4 views (last 30 days)
Show older comments
Hello everyone,
I am having trouble with reseting my variables for each iteration. So the main idea is this:
I have 6 measurements (Data series called 'OUTPUT',each one has 6 Columns: 1. time, 2. and 3. sound pressure, 4.,5. and 6. wind velocities xyz).
For each measurement I have to evaluate the mean, std and so on, so thats why I am dividing my signal into blocks of 5s.
For example, 1. Measurement lasts 40s, each block will be 5s. (Sample frequency 100Hz).
But each measurements doesnt have the same amount of blocks , because they dont last all 40s.
So outside the main evaluations I added a loop : for j=1:6, to choose everytime which measurement I want to read in.
And after choosing it, at the block parameter I added:
for j=1:6
%reading in the Input File Array
%Block parameters
blocks=[8 6 4 6 6 6]; %Block number for each measurement
i_blocks=blocks(j); %Block number for each measurement
n_block_res=floor(length(OUTPUT_resample)/i_blocks); %Number of samples for each block
t_block=n_block_res/fs_resample; %Duration of each block [s]
%Measurements
for ii=1:i_blocks
Mean(ii,:,j)=.... %here I added the j at the end so I will get a 3D matrix for each measurement j iteration
Std(ii,:,j)=....
........
end
end
So the problem is that, after the first iteration, the variables wont be overwriten and therefore I get an error because the size on the left side doesnt agree with the one on the right (as I said each measuement has different number of blocks).
So maybe the Mean has a size at measuement 1 , 8(blocks)x6, but the Mean at measurement 2, wont be 6(blocks)x6, but still 8x6.
I hope my problem is making sense. Thanks for your time and help!
15 Comments
Adam Danz
on 18 Aug 2019
Edited: Adam Danz
on 18 Aug 2019
I see. One way around that is to store the var1_temp and var2_temp in a cell array.
% Pre-allocate the variables that will save ALL of the data
var1 = cell(1,6);
var2 = cell(1,6);
for j = 1:6
% Pre-allocate the ii-loop variables
var1_temp = zeros(4,5);
var2_temp = zeros(4,5);
for ii = 1:4
var1_temp(ii,:) = func1();
var2_temp(ii,:) = func2();
end
% Store the values before going to next loop in J
var1{j} = var1_temp;
var2{j} = var2_temp;
end
Another solution would be to pre-allocate the var1 variables to the largest possible var1_temp size and then pad the smaller var1_temp matrices with NaN or 0s.
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!