For loop preallocation warning?
2 views (last 30 days)
Show older comments
Tony Castillo
on 15 Oct 2020
Commented: Ameer Hamza
on 16 Oct 2020
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 Comments
Steven Lord
on 15 Oct 2020
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?
Accepted Answer
Ameer Hamza
on 15 Oct 2020
Edited: Ameer Hamza
on 15 Oct 2020
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 Comments
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!