Matlab R2015b Radiobutton按代码选择

时间:2016-07-10 02:18:03

标签: matlab-gui

以下代码创建3个radiobuttons。选择功能可确保显示适当的内容:

function myui
bg = uibuttongroup('Visible','off',...
                  'Position',[0 0 .2 1],...
                  'SelectionChangedFcn',@bselection);

% Create three radio buttons in the button group.
r1 = uicontrol(bg,'Style','radiobutton',...
                  'String','Option 1',...
                  'Position',[10 350 100 30],...
                  'HandleVisibility','off');

r2 = uicontrol(bg,'Style','radiobutton',...
                  'String','Option 2',...
                  'Position',[10 250 100 30],...
                  'HandleVisibility','off');

r3 = uicontrol(bg,'Style','radiobutton',...
                  'String','Option 3',...
                  'Position',[10 150 100 30],...
                  'HandleVisibility','off');

% Make the uibuttongroup visible after creating child objects. 
bg.Visible = 'on';

    function bselection(source,callbackdata)
       display(['Previous: ' callbackdata.OldValue.String]);
       display(['Current: ' callbackdata.NewValue.String]);
       display('------------------');
    end
end

我想更改每个代码所显示的选择和内容(没有用户点击)。 我可以将每个代码的单选按钮选择设置为:

set(bg, 'SelectedObject', r2);

这会为正确的单选按钮绘制蓝点(似乎已选中),但是,未显示正确的内容。 我想我需要打电话给' SelectionChangedFcn'手工,我不能按照它要求的回调数据'这是一个' matlab.ui.eventdata.SelectionChangedData'没有文档的课程。

问题:我是否需要创建' callbackdata'我自己从外面打电话给SelectionChangedFcn'当编码radiobutton变化时,还是有另一种方式?

问题2 :我会在为什么/何时使用HandleVisibility'是否有用,MatLab文档对我来说太神秘了。

1 个答案:

答案 0 :(得分:0)

首先,关于问题2:如果您在单选按钮上将属性HandleVisibility设置为off,则无法访问uibuttongroup句柄中其句柄的句柄

例如,使用HandleVisibilityoff

bg.Children

ans = 

  0x0 empty GraphicsPlaceholder array.

使用HandleVisibilityon

bg.Children

ans = 

  3x1 UIControl array:

  UIControl    (Option 3)
  UIControl    (Option 2)
  UIControl    (Option 1)

这取决于您想要如何访问您的无线电按钮。

关于以编程方式调用回调函数,我建议您编写自己的回调函数,而不是使用SelectionChangedFcn(在这种情况下,您将HandleVisibility设置为on) 。 这个解决方案对我有用:

function myui

    figure
    bg = uibuttongroup('Visible','off',...
                      'Position',[0 0 .2 1]);

    % Create three radio buttons in the button group.
    r1 = uicontrol(bg,'Style','radiobutton',...
                      'String','Option 1',...
                      'Position',[10 350 100 30],...
                      'HandleVisibility','on');

    r2 = uicontrol(bg,'Style','radiobutton',...
                      'String','Option 2',...
                      'Position',[10 250 100 30],...
                      'HandleVisibility','on');

    r3 = uicontrol(bg,'Style','radiobutton',...
                      'String','Option 3',...
                      'Position',[10 150 100 30],...
                      'HandleVisibility','on');

    % Make the uibuttongroup visible after creating child objects. 
    bg.Visible = 'on';

    % Use here your own way to switch the buttons, I use a simple vector as a counter
    CounterToSelect = [1 2 3 1 2 3 1 2 3];
    for k=1:length(CounterToSelect);
        pause(1)
        previousChild = bg.SelectedObject;
        selectedChild = bg.Children(CounterToSelect(k));
        bg.SelectedObject = selectedChild;
        DisplayInfoChildren(previousChild, selectedChild);
    end

end

function DisplayInfoChildren(previousChild, selectedChild)
    display(['Previous: ' previousChild.String]);
    display(['Current: ' selectedChild.String]);
    display('------------------');
end