How fix speed of 3 For loops?

1 回表示 (過去 30 日間)
KEYVAN PEBDANI SAHRAEI
KEYVAN PEBDANI SAHRAEI 2021 年 4 月 4 日
回答済み: Animesh 2024 年 5 月 16 日
Hello
I have animated a proceess in GUI and used For loop to create animation of a mixing tank .However, the problem is my loop will work slow ecah ime more than before?
I am nott sure anyone know how to fix the problem?
Codes are provided below ,it is only to start button's function:
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
%set(handles.stop_Callback, 'userdata', 0);
set(handles.stop,'userdata',0);
set(handles.pause,'userdata',0);%defining the value of the pause-button
set(handles.emergency_stop,'userdata',0);%defining the value of the emergency-stop-button
set(handles.reset,'userdata',0);
if get(handles.reset,'userdata')% getting the response from Emergency-stop button
title('Process is reset') %creating title
return; %%stop process
end
axis([-10 10 -15 15])
for loop2=1:3 %it is a General Loop which loops all process
title('stop button is applied') %creating title
if get(handles.stop,'userdata')% getting the response from stop button
set(handles.stop,'userdata',1); %setting value of stop button
break; %stopping loop
end
if get(handles.pause,'userdata')%getting the response from pause button
uiwait(handles.figure1); %UIWAIT makes Guide wait for getting continue button's response
title(' process is paused') %creating title
end
if get(handles.emergency_stop,'userdata')% getting the response from Emergency-stop button
title('process is stopped as Emergency') %creating title
break; %Stopping loop
end
%...................plotting pipes/product A
x=[2.1 8]; % mentioning value of x
y=[9.5 9.5]; %mentioning value of y
plot(x,y,'Color','yellow','LineWidth',7)% drawing pipe
...................plotting pipes/product A
x1=[2.3 2.3]; % mentioning value of x
y1=[9.5 7]; %mentioning value of y
plot(x1,y1,'Color','yellow','LineWidth',7)% drawing pipe
................................filling animation
x1=[2.3 2.3]; % mentioning value of x
y1=[0 7]; %mentioning value of y
plot(x1,y1,'--','Color','yellow','LineWidth',7)% drawing pipe
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
level=0;% creating variable
level_h= randi(1)*25; %Percentage of tank volume
pause(0.001)
%Filling tank with product A/opening valve1........................................
for i=1:level_h % Filling tank with product A /Opening valve 1
pause(0.001)
if get(handles.pause,'userdata')%% getting the response from pause button
uiwait(handles.figure1); %UIWAIT makes Guide wait for getting continue button's response
end
if get(handles.reset,'userdata')% getting the response from rese-button
break;
end
level=level+0.15; %%%level of liquid
%%%%plotting level of liquid
patch([-4.4 4.15 4.15 -4.4],[ 0 0 level level]-7.05,'yellow');%Starting line
x5=[0 0];
y5=[-4 11];
plot(x5,y5,'Color','black','LineWidth',12)
hold on
x6=[-2 2];
y6=[-4 -4];
plot(x6,y6,'Color','black','LineWidth',12)
hold on
rectangle('Position',[-3 -5 2 2.25],'FaceColor','black')
rectangle('Position',[1 -5 2 2.25],'FaceColor','black')
pause(0.001) % delay time
W=8; %defining wiedth of the tank's body
L=8; %defining length of the tank's body
H=level; %defining height of the tank's body
V=(W*L*H); %computing volume of the tank
disp(V); %diplay the value of the tank
set(handles.text1,'String',V);
title(['Filling Tank// Vlave1 is open:',num2str(level_h),'%']); %writting Title
drawnow %animating procces
end
pause(0.001) %making delay
level_h=randi(1)*75; %Percentage of tank volume
....................plotting pipes
x=[2.1 8]; % mentioning value of x
y=[9.5 9.5]; % mentioning value of x
plot(x,y,'Color','blue','LineWidth',7) %Drawing pipe
...................plotting pipes
x1=[2.3 2.3]; % mentioning value of x
y1=[9.5 7]; % mentioning value of y
plot(x1,y1,'Color','blue','LineWidth',7) %Drawing pipe
.................removing aniamtion of product A
x1=[2.3 2.3]; % mentioning value of x
y1=[0 7]; % mentioning value of y
plot(x1,y1,'--','Color','white','LineWidth',7) %Drawing liquid
................plotting pipes/product B
x=[-2.1 -8]; % mentioning value of x
y=[9.5 9.5]; % mentioning value of y
plot(x,y,'Color','cyan','LineWidth',7) %Drawing liquid inside pip
................plotting pipes/product B
x1=[-2.3 -2.3]; % mentioning value of x
y1=[9.5 7]; % mentioning value of y
plot(x1,y1,'Color','cyan','LineWidth',7) %Drawing liquid inside pip
..........................animatting filling by product B
x1=[-2.3 -2.3]; % mentioning value of x
y1=[0 7]; % mentioning value of y
plot(x1,y1,'--','Color','cyan','LineWidth',7)%Drawing liquid inside tank
pause(0.01) % delay time
%%%%%%%%%%%%%%%%Filling tank with product B %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pause(2);
for i=6:level_h
if get(handles.pause,'userdata')% getting response from pause-button
uiwait(handles.figure1); %UIWAIT makes Guide wait for getting continue button's response
end
if get(handles.emergency_stop,'userdata') % getting the response from Emergency-stop button
title('process is stopped as Emergency') %creating title
break; %stopping loop
end
level=(level+0.150); %%%level stpoint
%plotting level of liquid >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
patch([-4.4 4.15 4.15 -4.4],[ 0 0 level-3.60 level-3.60]-3.70,'cyan');%Starting line
x5=[0 0]; % mentioning value of x
y5=[-4 11]; % mentioning value of y
plot(x5,y5,'Color','black','LineWidth',12)
x6=[-2 2]; % mentioning value of x
y6=[-4 -4]; % mentioning value of y
plot(x6,y6,'Color','black','LineWidth',12)
rectangle('Position',[-3 -5 2 2.25],'FaceColor','black')
rectangle('Position',[1 -5 2 2.25],'FaceColor','black')
if get(handles.reset,'userdata')% getting the response from reset-button
break; %stopping loop
end
pause(0.001) % delay time
%%%.............computing vloume of Tank
H=level; %Height of tank's body
W=8; %weidth of tank's body
L=8; %lenght of tank's body
V=((W*L*H)); %computting the volume of the liquid
disp(V); %display value of the volume
set(handles.text1,'String',V);% setting value of text box
title(['Filling Tank//valve1 closed// Valve2 is open//Mixer is ON:',num2str(level_h),'%']); %creating Title
drawnow %animating procces
rectangle('Position',[-3 -5 2 2.25],'FaceColor','red');
rectangle('Position',[1 -5 2 2.25],'FaceColor','red');
pause(0.01)% making delay
rectangle('Position',[-3 -5 2 2.25],'FaceColor','black');
rectangle('Position',[1 -5 2 2.25],'FaceColor','black');
drawnow%animating process
end
%%%%%%%%%Starting to Empty Tank%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pause(1);%making delay
title('EMPTY TANK// Valve2,valve1 are closed ,Drain valve is open now')% making titile
for i=10:level_h % draining tank's Loop
if get(handles.pause,'userdata')%getting the response from pause button
uiwait(handles.figure1); %UIWAIT makes Guide wait for getting continue button's response
end
if get(handles.emergency_stop,'userdata') % getting the response from Emergency-stop button
break; %stopping loop
end
if get(handles.reset,'userdata')% getting the response from Emergency-stop button
break;%stopping loop
end
H=level; %Height of tank's body
W=8; %weidth of tank's body
L=8; %lenght of tank's body
V=((W*L*H)); %computting the volume of the liquid
disp(V); %display value of the volume
................plotting pipes/product B
x=[-2.1 -8]; % mentioning value of x
y=[9.5 9.5]; % mentioning value of y
plot(x,y,'Color','blue','LineWidth',7) %drawing pipe
................plotting pipes/product B
x1=[-2.3 -2.3]; % mentioning value of x
y1=[9.5 7]; % mentioning value of y
plot(x1,y1,'Color','blue','LineWidth',7) %drawing pipe
.....................plotting pipes/Drain pipes
x2=[0 0]; % mentioning value of x
y2=[-10 -7.05]; % mentioning value of y
plot(x2,y2,'--','Color','green','LineWidth',7)%DRAWING PIPE
....................plotting pipes/Drain pipes
x3=[-0.2 8]; % mentioning value of x
y3=[-10 -10]; % mentioning value of y
plot(x3,y3,'--','Color','green','LineWidth',7) %DRAWING PIPE
pause(0.01)%making delay
%plotting empty Tank>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
patch([-4.4 4.15 4.15 -4.4],[0 0 level level]-7.05,[1 1 1],'Edgecolor','white');%Ending level/plotting components
level=level-0.2150;% setting tank's level
patch([-4.4 4.15 4.15 -4.4],[0 0 level level]-7.05,'green');%Starting level
%.................
H=level; %Height of tank's body
W=8; %weidth of tank's body
L=16; %lenght of tank's body
V=((W*L*H)); %computting the volume of the liquid
disp(V); %display value of the volume
set(handles.text1,'String',V);
%....................................plotting Mixer
x5=[0 0]; % mentioning value of x
y5=[-4 11]; % mentioning value of y
plot(x5,y5,'Color','black','LineWidth',12)
hold on % keeping visible
x6=[-2 2]; % mentioning value of x
y6=[-4 -4];% mentioning value of y
plot(x6,y6,'Color','black','LineWidth',12)%plotting mixer
hold on % keeping visible
rectangle('Position',[-3 -5 2 2.25],'FaceColor','black')%plotting mixer
rectangle('Position',[1 -5 2 2.25],'FaceColor','black') %plotting mixer
pause(0.001)% delay time
drawnow%animating process
end
title(' Drain-valve closed-End of Process')% making title
.....................plotting pipes/Drain pipes
x2=[0 0]; % mentioning value of x
y2=[-10 -7.05]; % mentioning value of y
plot(x2,y2,'Color','blue','LineWidth',12)
....................plotting pipes/Drain pipes
x3=[-0.1 8]; % mentioning value of x
y3=[-10 -10]; % mentioning value of y
plot(x3,y3,'Color','blue','LineWidth',12)
clc; %cleaning the command window
if get(handles.emergency_stop,'userdata') % getting the response from Emergency-stop button
title('process is stopped as Emergency') %creating title
break; %stopping loop
end
if get(handles.reset,'userdata')%getting the response from reset-button
title('Process is reset') %creating title
break; %stopping loop
end
drawnow%animating process
end
return;

回答 (1 件)

Animesh
Animesh 2024 年 5 月 16 日
Hello Keyvan,
It seems like your code is slowing down due to the extensive use of pause and drawnow functions inside your loops. These functions are known to slow down the execution of loops in MATLAB. Here are a few suggestions that might help:
  1. Minimize the use of pause and drawnow: These functions force MATLAB to halt execution or update figures, which can be time-consuming, especially in a loop. If possible, try to call these functions less frequently.
  2. Preallocate Memory: If you’re working with arrays or matrices, preallocating memory can significantly improve performance. For example, if you’re working with an array that grows with each iteration, predefine the size of the array before the loop.
To read more about preallocation, please refer to the following link -https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html
I hope I was able to help you.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by