按钮按下状态属性GUI MATLAB

问题描述 投票:0回答:1

被推送时,是否有可能更改btn1值或其状态/属性?

例如:

我的GUI中有2个按钮btn1和btn2。当我单击btn1时,其本身的值/标志/状态会自动更改为另一个值(也许为true / 1),因此我可以在代码中使用它来执行一些操作。

是否可以选择?除了全局值。

我真的很想通过编程来做到这一点,并尽可能避免使用appdesigner。

matlab properties uibutton matlab-gui
1个回答
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

代替显示所按下的按钮,您当然应该运行自己的方案。

希望这会有所帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.