For loop preallocation warning?

5 ビュー (過去 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