Why does the first value in loop get overwritten

조회 수: 1 (최근 30일)
Sargondjani
Sargondjani 2012년 12월 8일
For some reason when fi == 1, NPV_list(1,1) is inserted correctly, but when fi=2 the value gets overwritten, which i dont understand... at all. This is the code:
clear all;
clc;
Fi = [0,0.8:0.05:1];
T = 160;
NPV_list = NaN(length(Fi),1);
for fi = 1:length(Fi);
fi
F = Fi(fi);
load (['res_us_v15_',num2str(T),'_',num2str(100*F)])
NPV_list(fi,1) = us.sim.NPV_Pb_Wlth_avg
clearvars -except NPV_list Fi T;
end
Can someone explain this? This is the output for first 2 iterations:
fi =
1
NPV_list =
9.9570
NaN
NaN
NaN
NaN
NaN
fi =
2
NPV_list =
9.8664
NaN
NaN
NaN
NaN
NaN

채택된 답변

per isakson
per isakson 2012년 12월 8일
편집: per isakson 2012년 12월 8일
No, I cannot explain. However, what else is in the files you load? The clearvars looks scary to me.
My function, cssm, works:
>> cssm()
ans =
101 102 103 104 105 106
where
function NPV_list = cssm()
Fi = [0,0.8:0.05:1];
T = 160;
us = [ 101 : 109 ];
NPV_list = NaN(1,length(Fi));
for fi = 1:length(Fi);
fi;
F = Fi(fi);
NPV_list(1,fi) = us(fi);
clearvars -except NPV_list Fi T us;
end
end
.
Doc says:
S = load( filename, variables );
Try
S = load( filename, 'us' );
and remove clearvars.
Last, check whether there is a variable in the loaded file that explains the behavior you see.

추가 답변 (2개)

Richard
Richard 2012년 12월 8일
편집: Richard 2012년 12월 8일
Try
Fi = [0,0.8:0.05:1];
T = 160;
NPV_list = NaN(length(Fi),1);
for fi = 1:length(Fi);
F = Fi(fi);
load (['res_us_v15_',num2str(T),'_',num2str(100*F)])
NPV_list(fi,1) = us.sim.NPV_Pb_Wlth_avg
end
clearvars -except NPV_list Fi T;
I wouldn't clear the variables until after the loop. It might be the case that by clearing Fi and T, it cannot read it for loop 2 thus no output.

Sargondjani
Sargondjani 2012년 12월 9일
Thanks guys... normally i never save iteration variables like 'fi' but in this case the files contained "fi = 1". very silly

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by