Push Button help!

4 次查看(过去 30 天)
Priyana Kumar
Priyana Kumar 2021-4-18
Hello, I am trying to make two push buttons in MATLAB in which one allows me to quit the program and the other allows me to continue running it.
This is what I have as my function:
function ButtonQuit
message = sprintf("Are you sure you would like to quit gesture recognition?");
figquit = uifigure('Position', [450 150 395 175]);
tarea = uitextarea(figquit);
tarea.Value = message;
%movegui('southeast')
%uialert(figquit,message,'Warning','Icon','warning');
% Create a push button %left bottom width height
btn1 = uibutton(figquit,'push',...
'Position',[50, 20, 150, 50],...
'ButtonPushedFcn', @(btn,event) Button1(btn1));
btn1.Text = 'Yes, Quit';
btn2 = uibutton(figquit,'push',...
'Position',[250, 20, 150, 50],...
'ButtonPushedFcn', @(btn,event) Button2(btn2));
btn2.Text = 'No';
if Button1(btn1) %if btn1, confirm quit
return;
elseif Button2(btn2) %if btn2, close window, continue gesture recognition
close(fig);
end
end
I'm not sure if something is missing or I have the wrong idea entirely.

回答(1 个)

Divija Aleti
Divija Aleti 2021-4-21
Hi Priyana,
There is a slight change in syntax that you need to make for the ButtonPushedFcn.
Assuming that when Button 1 is pushed, nothing should happen, and when Button 2 is pushed, the figure should close, I corrected the syntax as follows :
function ButtonQuit
message = sprintf("Are you sure you would like to quit gesture recognition?");
figquit = uifigure('Position', [450 150 395 175]);
tarea = uitextarea(figquit);
tarea.Value = message;
%movegui('southeast')
%uialert(figquit,message,'Warning','Icon','warning');
% Create a push button %left bottom width height
btn1 = uibutton(figquit,'push',...
'Position',[25, 20, 150, 50],...
'ButtonPushedFcn', @(btn,event) Button1);
btn1.Text = 'Yes, Quit';
btn2 = uibutton(figquit,'push',...
'Position',[225, 20, 150, 50],...
'ButtonPushedFcn', @(btn,event) Button2);
btn2.Text = 'No'
function Button1(btn, event)
return;
end
function Button2(btn, event)
close(figquit);
end
end
If you need an example on how to create a quit button, have a look at the following link:
Hope this helps !
Regards,
Divija

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by