필터 지우기
필터 지우기

For loop preallocation warning?

조회 수: 6 (최근 30일)
Tony Castillo
Tony Castillo 2020년 10월 15일
댓글: Ameer Hamza 2020년 10월 16일
Hello all,
Even the code works properly, I get a warning flag that advice me to preallocate all of the variables that I am accumulating during the execution of the code, do you know how to deal with this issue?, in order to debug and make faster the execution of the code.
Thanks in advance!.
%To simulate the 12 months at one click
current_path = pwd;
tic
load DATA_HILL.mat
PV_MAX_=[];
DEV_PV_=[];
SOC_=[];
month_=[];
PV_irr_=[];
Dev_2_=[];
z=zeros(12,6);
for month_number=1:12
sim('HILL_V2.slx')
disp(month_number);
bdclose('HILL_V2.slx');
%%%%%%
%%%%Data upload
load Power_PV.mat
load SOC.mat
load month
load Irr
%%%Results format
format Eng
%%%%Variables analysed in this study case
PV_MAX=max(Power_PV);
%
DEV_PV= (PV_MAX/189000)*100;
%
SOC= min(SOC);
m=mean(month);
PV_irr=max(Irr)*189;
Dev_2=(1-(PV_MAX/PV_irr))*100;
% % % % All of the accumulated variables in the for loop
PV_MAX_=[PV_MAX; PV_MAX_];
DEV_PV_=[DEV_PV; DEV_PV_];
SOC_=[SOC; SOC_];
month_=[m; month_];
PV_irr_=[PV_irr; PV_irr_];
Dev_2_=[Dev_2; Dev_2_];
cd(current_path);
end
A=flip([month_ SOC_ PV_MAX_ DEV_PV_ PV_irr_ Dev_2_].*z);
%%%%To creat a table in order to represent the results of the simulation
% RowName={'Month', 'SOC_min(%)', 'PV_MAX (kW)', 'DEV_PV (%)', 'PV_irr (kW)', 'Dev_2 (%)'};
LastName=flip({'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'});
T=table(month_, SOC_, PV_MAX_, DEV_PV_, PV_irr_, Dev_2_,...
'RowName', LastName);
disp(T)
toc
  댓글 수: 2
Steven Lord
Steven Lord 2020년 10월 15일
In the interest of improving the documentation, was there any part of the documentation page "Preallocating Arrays" linked at the end of the first paragraph in the "Suggested Action" section of the Code Analyzer message that was unclear or that you expected to contain more information?
Tony Castillo
Tony Castillo 2020년 10월 15일
In and overall view it results some confusing to me, maybe because I implemented and the warning is still on.

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 15일
편집: Ameer Hamza 2020년 10월 15일
Here is a general way.
If you use the following codes, you will get a warnings
x = []
for i = 1:10
x(i) = sin(i); % sin(i) used as example
end
% or
x = []
for i = 1:10
x = [x sin(i)]
end
You can solve the warning by pre-allocating and use indexing to assign elements
x = zeros(1, 10)
for i = 1:10
x(i) = sin(i); % sin(i) used as example
end
For your code, following will work
%To simulate the 12 months at one click
current_path = pwd;
tic
load DATA_HILL.mat
PV_MAX_=zeros(12,1);
DEV_PV_=zeros(12,1);
SOC_=zeros(12,1);
month_=zeros(12,1);
PV_irr_=zeros(12,1);
Dev_2_=zeros(12,1);
z=zeros(12,6);
for month_number=1:12
sim('HILL_V2.slx')
disp(month_number);
bdclose('HILL_V2.slx');
%%%%%%
%%%%Data upload
load Power_PV.mat
load SOC.mat
load month
load Irr
%%%Results format
format Eng
%%%%Variables analysed in this study case
PV_MAX=max(Power_PV);
%
DEV_PV= (PV_MAX/189000)*100;
%
SOC= min(SOC);
m=mean(month);
PV_irr=max(Irr)*189;
Dev_2=(1-(PV_MAX/PV_irr))*100;
% % % % All of the accumulated variables in the for loop
PV_MAX_(i)=PV_MAX
DEV_PV_(i)=DEV_PV;
SOC_(i)=SOC;
month_(i)=m;
PV_irr_(i)=PV_irr;
Dev_2_(i)=Dev_2;
cd(current_path);
end
A=flip([month_ SOC_ PV_MAX_ DEV_PV_ PV_irr_ Dev_2_].*z);
%%%%To creat a table in order to represent the results of the simulation
% RowName={'Month', 'SOC_min(%)', 'PV_MAX (kW)', 'DEV_PV (%)', 'PV_irr (kW)', 'Dev_2 (%)'};
LastName=flip({'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'});
T=table(month_, SOC_, PV_MAX_, DEV_PV_, PV_irr_, Dev_2_,...
'RowName', LastName);
disp(T)
toc
  댓글 수: 4
Tony Castillo
Tony Castillo 2020년 10월 16일
Thank you for your help.
Ameer Hamza
Ameer Hamza 2020년 10월 16일
I am glad to be of help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by