在推送时是否有可能更改btn1值或某种状态/属性?
例如:
我的GUI中有2个按钮btn1和btn2。当我单击btn1时,其本身的值/标志/状态会自动更改为另一个值(也许为true / 1),因此我可以在代码中使用它来执行一些操作。
是否可以选择?除了全局值。
我真的很想通过编程来做到这一点,并尽可能避免使用appdesigner。
答案 0 :(得分:0)
您有使用回调函数的经验吗?
您可以尝试运行此程序并将其应用于您的问题。
function buttonTest
% create figure
fig = figure;
% create first push button
btn1 = uicontrol(...
'Parent',fig,...
'style','pushbutton',...
'units','normalized',...
'Position',[0.1 0.15 0.8 0.2],...
'String','BTN 1',...
'Callback',@myfunction);
% create second push button
btn2 = uicontrol(...
'Parent',fig,...
'style','pushbutton',...
'units','normalized',...
'Position',[0.1 0.65 0.8 0.2],...
'String','BTN 2',...
'Callback',@myfunction);
% Generate loop to automatically run function behind push buttons
for i = 1:5
myfunction(btn1)
pause(1)
myfunction(btn2)
pause(1)
end
%% Callback function
function myfunction(source,~)
% detect source from call and display it.
if strcmpi(source.String,'BTN 1')
disp('BTN 1 pushed')
elseif strcmpi(source.String,'BTN 2')
disp('BTN 2 pushed')
end
end
end
您当然应该运行自己的方案,而不是显示按下的按钮。