How to output a variable from a loop as a large array

I'm hoping to output the variable 'fluxset' from a loop into an array which would store all the outputs into one large array.
I have tried a fluxset(month)=... array storage method to no luck, I also tried outputting the variable as a series of .mat files using the sprintf function to name the files however the variable names were all the same and I could not fix this issue due to a supposed 'dynamic variable naming' issue.
My code is approx. in the form:
for year=[1993:2007]
for month=[1:12]
...
fluxset=[fluxa;fluxb;fluxc]
end
end
I hope to either store an all loop outputs array or alternatively a year by year output array.
If anyone could help, I would be greatly appreciative.

5 commentaires

save( sprintf( 'my_flux_%4u.mat', year ), 'fluxset' )
Josh
Josh le 12 Juil 2012
Unfortunately, I believe that would just save each individual monthly output with the name in reference to the year.
However, what I would like to do is output all the monthly outputs into an array or preferably all the outputs for each month within each year into one large array.
Conrad
Conrad le 12 Juil 2012
What is the dimensions of [fluxa;fluxb;fluxc]?
Conrad
Josh
Josh le 12 Juil 2012
fluxa... are all 1x1, so subsequently fluxset is 3x1
Could try:
years = 1993:2007;
months = 1:12;
fluxset = zeros(numel(years),numel(months),3);
for year = years
for month = months
fluxset(year-years(1)+1,month-months(1)+1,:) = [1 1 1]';
end
end
Note that I used dummy values [1 1 1]' in place of [fluxa;fluxb;fluxc].
Conrad

Connectez-vous pour commenter.

 Réponse acceptée

If I understand you right - I THINK you want the information from each loop to be added on to the array right? If so, this could work but creating a different variable - one that gets recomputed with each loop, adn then the "master" variable that gets the recomputed varible added on to it with each loop:
fluxset = [];
for year=[1993:2007]
for month=[1:12]
fluxset0=...%Computations here;
fluxset = [fluxset; fluxset0];
end

1 commentaire

Josh
Josh le 13 Juil 2012
Thank you Michelle and all who have assisted, you've saved me hours of mind-numbing file editing.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 12 Juil 2012
And with the important pre-allocation:
n = (2007 - 1993 + 1) * 12;
fluxset = zeros(n, 3); % Pre-allocate!
index = 0;
for year = 1993:2007
for month=[1:12]
...
index = index + 1;
fluxset(index, :) = [fluxa, fluxb, fluxc];
end
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by