I needed to create a variable that have a dynamic name, and assign values to it. Since eval is bad practice, I'm using the structure method. However, it's still reducing the performnace very badly.
hf=figure;
createnewvar=1; %this value will be changing depends on external factor
tempvar=zeros(500,800,3,500);
varname='var1'; %this value will be changing depends on external factor
idx=1;
while ishandle(hf)
if createnewvar
Main.(varname)=zeros(500,800,3,500);
createnewvar=0;
end
somevalue=rand(500,800,3);
tic
tempvar(:,:,:,idx)=somevalue; %This line is the bottleneck
tempvar_t=toc
tic
Main.(varname)=tempvar; %This line too
Main_t=toc
idx=idx+1;
end
And the time taken:
tempvar_t =
2.5747
Main_t =
0.5584
Any better solution to handle this? Thanks!