uiprogressdlg using the CancelRequested function to terminate the loop.

6 visualizaciones (últimos 30 días)
I currently have an App Designer program set up to run calculations in a loop to return incremental data. While running the loop, I'd like to have a kill all button which will cause the function to stop incase there is an issue with any of the hardware. However, I can't seem to trigger the cancel button on my porgress bar. All I want it to do is to terminate the serial connection to my hardware LDS1000 which should eventually lead to teh fucntion stoppping on the next iteration of the loop as it will no longer be able to write/read any data, but whatever it is that I try I can't seem to get the cancel button to work. I have attached the function that I am trying to stop with the CancelRequested button most of the code is data retrieval and processing however the part I am instersted is the uiprogressdlg function and where to insert it in to the code etc.
Any help with this issue would be massively appreciated! I'm sure there's a simple fix that I'm missing.
function Plot(app)
for index = 1:1:app.n
pause(1.5)
writeline(app.LDS1000,'1TP'); % Measure the current value in the Y axis
data_y = readline(app.LDS1000); % Read the current value in the Y axis (with the command aatached to it) e.g. TP-1.2 or TP0.4
k_1 = [] ;
k_1 = strfind(data_y,'-','ForceCellOutput',true); % When the data is read and it isn't a 0.y value there is a dash between the command 'TP' and the numerical value,
if k_1{1} == 4 % hence the characters are separated at the dash, so the dash will be the 4th value hence k_1 == 4
d_1 = regexp(data_y,'\-','split'); % extract the numerical value
y = str2double(d_1); % convert in to a double
pause (0.5)
% However, when the data is extracted and it is a 0.y value this doesn't include a dash between the command 'TP' and the numercial value, hence the characters are separated at P
else k_1{1} == 0; %hence the characters are separated at P instead and this only occurs when k does not equal 4 as teh dash doesn't exist hence it is not the value
d_1 = regexp(data_y,'\P','split'); % extract the numerical value
y = str2double(d_1); % convert in to a double
pause (0.5)
end
writeline(app.LDS1000,'2TP'); % Repeat the whole precoess above however now for Y
data_z = readline(app.LDS1000);
k_2 = [] ;
k_2 = strfind(data_z,'-','ForceCellOutput',true);
if k_2{1} == 4;
d_2 = regexp(data_z,'\-','split');
z = str2double(d_2);
pause (0.5)
else k_2{1} == 0;
d_2 = regexp(data_z,'\P','split');
z = str2double(d_2);
pause (0.5)
end
% Reading curent vlues from Heidenhain
write(app.ND280,app.ESC,"char");
write(app.ND280,app.A0100_p,"char");
write(app.ND280,app.CR,"char");
Heid_x = read(app.ND280,16,"uint8");
Heid_X_2 = char(Heid_x);
Heid_Out = string(Heid_X_2);
H_1 = [];
H_1 = regexp(Heid_Out,'\s','split');
if H_1{1,5} ~= ""
H_2 = H_1{1,5};
H_3 = eraseBetween(H_2,6,6);
H_4 = str2double(H_3);
else H_1{1,4} ~= ""
H_2 = H_1{1,4};
H_3 = eraseBetween(H_2,7,7);
H_4 = str2double(H_3);
end
app.X_Heid(1,index) = H_4;
% Plotting and interpolating the read values
if index == 1 % To interpolate it requires to values in X and Y/Z
app.Z(1) = z(1,2); % hence, the initial values for Y/Z are just
app.Y(1) = y(1,2); % plotted which are Z(1) and Y(1)
xlim(app.UIAxes,[-app.s app.s]); % X limit is 50 to -50 on the figure
ylim(app.UIAxes,[-app.s app.s]); % Y limit id 50 to -50 on the figure
title(app.UIAxes,'Reflection Angle in Y and Z vs Stroke');
xlabel(app.UIAxes,'Slide Translation mm');
ylabel(app.UIAxes,'Reflection Angle \mu\theta');
h = animatedline(app.UIAxes,app.X(1,index),app.Y(1,index),'Color','red','LineStyle','-','Marker','*','MarkerSize',2);
g = animatedline(app.UIAxes,app.X(1,index),app.Z(1,index),'Color','green','LineStyle','-','Marker','o','MarkerSize',2);
addpoints(h,app.X(1,1),app.Y(1,1)); % Y(1) values plotted
addpoints(g,app.X(1,1),app.Z(1,1)); % Z(1) values plotted
drawnow;
else index >= 2; % Once an initial value has been obatined all future
app.Z(index) = z(1,2); % nodes will have a previous node, hence the Y/Z
app.Y(index) = y(1,2); % for the current node are read
xx1 = linspace(app.X(1,index),app.X(1,(index-1)),2); % Creating the sample points between the current node and the previous one
yy1 = interp1(app.X(1,(index-1):index),app.Y(1,(index-1):index),xx1); % Interpolating between the current node and the previous one in Y
zz1 = interp1(app.X(1,(index-1):index),app.Z(1,(index-1):index),xx1); % Interpolating between the current node and the previous one in Z
for ci= 2:-1:1
addpoints(h,xx1(ci),yy1(ci)); % Adding the interpolated points for Y
addpoints(g,xx1(ci),zz1(ci)); % Adding the interpolated points for Z
pause(0.1);
end
end
h = animatedline(app.UIAxes,app.X(1,index),app.Y(1,index),'Color','red','LineStyle','-','Marker','*','MarkerSize',2);
g = animatedline(app.UIAxes,app.X(1,index),app.Z(1,index),'Color','green','LineStyle','-','Marker','o','MarkerSize',2);
hold off
legend(app.UIAxes,{'Y','Z'});
drawnow
% Displaying current vlaues:
%y
y_current = num2str(app.Y(index));
app.YEditField.Value = y_current;
%z
z_current = num2str(app.Z(index));
app.ZEditField.Value = z_current;
%x_ACS
x_current = num2str(app.X(index));
app.XACSmmEditField.Value = x_current;
%x_Heid
x_Heid_current = num2str(app.X_Heid(index));
app.XHeidmmEditField.Value = x_Heid_current;
pause(1)
di = uiprogressdlg(app.progress,'Title','Progress Bar','Message','Progress','Cancelable',1);
di.Value = index/app.n;
Value = round(di.Value*100);
Vally = num2str(Value);
percent = '%';
if di.CancelRequested == 1
close(di)
app.LDS1000 = [];
elseif di.CancelRequested == 0
break
end
end
end

Respuesta aceptada

Kevin Holly
Kevin Holly el 27 de Abr. de 2023
Everytime you run the line
di = uiprogressdlg(app.progress,'Title','Progress Bar','Message','Progress','Cancelable',1);
A new uiprogressdlg pops up and di.CancelRequested becomes 0.
Place this outside of the for loop as shown below. I have also attached an app as a proof of concept.
function Plot(app)
di = uiprogressdlg(app.progress,'Title','Progress Bar','Message','Progress','Cancelable',1);
for index = 1:1:app.n
pause(1.5)
writeline(app.LDS1000,'1TP'); % Measure the current value in the Y axis
data_y = readline(app.LDS1000); % Read the current value in the Y axis (with the command aatached to it) e.g. TP-1.2 or TP0.4
k_1 = [] ;
k_1 = strfind(data_y,'-','ForceCellOutput',true); % When the data is read and it isn't a 0.y value there is a dash between the command 'TP' and the numerical value,
if k_1{1} == 4 % hence the characters are separated at the dash, so the dash will be the 4th value hence k_1 == 4
d_1 = regexp(data_y,'\-','split'); % extract the numerical value
y = str2double(d_1); % convert in to a double
pause (0.5)
% However, when the data is extracted and it is a 0.y value this doesn't include a dash between the command 'TP' and the numercial value, hence the characters are separated at P
else k_1{1} == 0; %hence the characters are separated at P instead and this only occurs when k does not equal 4 as teh dash doesn't exist hence it is not the value
d_1 = regexp(data_y,'\P','split'); % extract the numerical value
y = str2double(d_1); % convert in to a double
pause (0.5)
end
writeline(app.LDS1000,'2TP'); % Repeat the whole precoess above however now for Y
data_z = readline(app.LDS1000);
k_2 = [] ;
k_2 = strfind(data_z,'-','ForceCellOutput',true);
if k_2{1} == 4;
d_2 = regexp(data_z,'\-','split');
z = str2double(d_2);
pause (0.5)
else k_2{1} == 0;
d_2 = regexp(data_z,'\P','split');
z = str2double(d_2);
pause (0.5)
end
% Reading curent vlues from Heidenhain
write(app.ND280,app.ESC,"char");
write(app.ND280,app.A0100_p,"char");
write(app.ND280,app.CR,"char");
Heid_x = read(app.ND280,16,"uint8");
Heid_X_2 = char(Heid_x);
Heid_Out = string(Heid_X_2);
H_1 = [];
H_1 = regexp(Heid_Out,'\s','split');
if H_1{1,5} ~= ""
H_2 = H_1{1,5};
H_3 = eraseBetween(H_2,6,6);
H_4 = str2double(H_3);
else H_1{1,4} ~= ""
H_2 = H_1{1,4};
H_3 = eraseBetween(H_2,7,7);
H_4 = str2double(H_3);
end
app.X_Heid(1,index) = H_4;
% Plotting and interpolating the read values
if index == 1 % To interpolate it requires to values in X and Y/Z
app.Z(1) = z(1,2); % hence, the initial values for Y/Z are just
app.Y(1) = y(1,2); % plotted which are Z(1) and Y(1)
xlim(app.UIAxes,[-app.s app.s]); % X limit is 50 to -50 on the figure
ylim(app.UIAxes,[-app.s app.s]); % Y limit id 50 to -50 on the figure
title(app.UIAxes,'Reflection Angle in Y and Z vs Stroke');
xlabel(app.UIAxes,'Slide Translation mm');
ylabel(app.UIAxes,'Reflection Angle \mu\theta');
h = animatedline(app.UIAxes,app.X(1,index),app.Y(1,index),'Color','red','LineStyle','-','Marker','*','MarkerSize',2);
g = animatedline(app.UIAxes,app.X(1,index),app.Z(1,index),'Color','green','LineStyle','-','Marker','o','MarkerSize',2);
addpoints(h,app.X(1,1),app.Y(1,1)); % Y(1) values plotted
addpoints(g,app.X(1,1),app.Z(1,1)); % Z(1) values plotted
drawnow;
else index >= 2; % Once an initial value has been obatined all future
app.Z(index) = z(1,2); % nodes will have a previous node, hence the Y/Z
app.Y(index) = y(1,2); % for the current node are read
xx1 = linspace(app.X(1,index),app.X(1,(index-1)),2); % Creating the sample points between the current node and the previous one
yy1 = interp1(app.X(1,(index-1):index),app.Y(1,(index-1):index),xx1); % Interpolating between the current node and the previous one in Y
zz1 = interp1(app.X(1,(index-1):index),app.Z(1,(index-1):index),xx1); % Interpolating between the current node and the previous one in Z
for ci= 2:-1:1
addpoints(h,xx1(ci),yy1(ci)); % Adding the interpolated points for Y
addpoints(g,xx1(ci),zz1(ci)); % Adding the interpolated points for Z
pause(0.1);
end
end
h = animatedline(app.UIAxes,app.X(1,index),app.Y(1,index),'Color','red','LineStyle','-','Marker','*','MarkerSize',2);
g = animatedline(app.UIAxes,app.X(1,index),app.Z(1,index),'Color','green','LineStyle','-','Marker','o','MarkerSize',2);
hold off
legend(app.UIAxes,{'Y','Z'});
drawnow
% Displaying current vlaues:
%y
y_current = num2str(app.Y(index));
app.YEditField.Value = y_current;
%z
z_current = num2str(app.Z(index));
app.ZEditField.Value = z_current;
%x_ACS
x_current = num2str(app.X(index));
app.XACSmmEditField.Value = x_current;
%x_Heid
x_Heid_current = num2str(app.X_Heid(index));
app.XHeidmmEditField.Value = x_Heid_current;
pause(1)
di.Value = index/app.n;
Value = round(di.Value*100);
Vally = num2str(Value);
percent = '%';
if di.CancelRequested == 1
close(di)
app.LDS1000 = [];
elseif di.CancelRequested == 0
break
end
end
end
  1 comentario
Joseph
Joseph el 27 de Abr. de 2023
Hi Kevin,
That worked a dream thank you very much for your assistance in this matter.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Mobile Fundamentals en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by