This question is closed. Reopen it to edit or answer.

Loop overwritting variables before complete layed out tasks ... Should be a simple spot but I am missing something!

1 view (last 30 days)
Hey everyone,
This is the script I have thus far:
inpdata = log(tower_wind_GF_2001);
inpdata = inpdata(~any(isnan(inpdata),2),:);
inpdata = inpdata(~any(isinf(inpdata),2),:);
names = {'trend10' 'trend20' 'trend40' 'trend80' 'trend120' 'trend200'};
names2 = {'slope10' 'slope20' 'slope40' 'slope80' 'slope120' 'slope200'};
for i = linspace(2,12,6);
for j = linspace(3,13,6);
for varname = 1:length(names)
for varname2 = 1:length(names2)
p = polyfit(inpdata(:,i), inpdata(:,j), 1);
slope = p(1);
int = p(2);
trend = slope*inpdata(:,i) + int;
trend = real(trend);
trends.(names{varname}) = trend;
slopes.(names2{varname2}) = slope;
Where the inpdata matrix has consecutive columns holding two parameters at 6 different heights. For instance,
are the two parameters for the 10 m height.
The script however will the same vector for trend10 and trend20 etc. Do I have to preallocate or store the vectors in a cell array before? I was under the assumption that the loop would run through all calculations, assign the variable name and then move onto the second value in i,j and k but clearly I am wrong.

Answers (1)

Sean de Wolski
Sean de Wolski on 5 Aug 2014
Avoid assigning individual variables to each piece and instead store them in a cell array you can index into:

Community Treasure Hunt

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

Start Hunting!

Translated by