【问题标题】:Using a radio button panel to choose different graphing options使用单选按钮面板选择不同的图形选项
【发布时间】:2009-07-21 09:42:00
【问题描述】:

我正在尝试创建一个 GUI,它将接受多个输入并通过多个函数运行它们。我希望使用单选按钮面板在不同的图表之间切换,但我似乎无法让它工作。这是我的代码示例。

switch get(eventdata.NewValue,'Tag')   % Get Tag of selected object
    case 'button1'
        status1 = str2double(get(handles.button1,'Value'));
        if status1 == 1;
            axes(handles.axes1)

            grid on;
            plot(x1,y1)

        end
    case 'button2'
        status2 = str2double(get(handles.button2,'Value'));
        if status2 == 1;
            axes(handles.axes1)

            grid on;
            plot(x2,y2)
        end

    case 'button3'
        status3 = str2double(get(handles.button3,'Value'));
        if status3 ==1
            plot(x3,y3)
        end

    otherwise
        % Code for when there is no match.

end

【问题讨论】:

  • 这是什么语言?将其添加到标签中,您将获得更多回复。

标签: matlab radio-button


【解决方案1】:

您似乎正在尝试以类似于this example tutorial on blinkdagger.com 的方式创建radio button panel。具体来说,我相信您正在尝试创建一个SelectionChangeFcn 来定义单选按钮如何修改您的 GUI。我建议如下:

首先,我建议您在创建 GUI 时绘制所有线条,然后将线条的“Visible”属性调整为“on”或“关闭”取决于选择的按钮。制作 GUI 时,可以在代码中的某处添加这些行(在创建轴并将其放置在 handles 变量中之后):

handles = guidata(hObject);  % Retrieve handles structure for GUI
set(handles.axes1,'NextPlot','add');  % Set axes to allow multiple plots
lineHandles = [plot(handles.axes1,x1,y1,'Visible','off') ...
               plot(handles.axes1,x2,y2,'Visible','off') ...
               plot(handles.axes1,x3,y3,'Visible','off')];
handles.lineHandles = lineHandles;  % Update handles structure
guidata(hObject,handles);  % Save handles structure

这将在同一轴上绘制三组线。这些线最初是不可见的,并且每条绘制线的句柄都收集在矢量变量lineHandles 中。上面的最后两行将线句柄添加到句柄结构中并更新 GUI 数据(hObject 应该是 GUI 图形窗口的句柄!)。

现在,您可以将以下内容用于您的 SelectionChangeFcn:

handles = guidata(hObject);  % Retrieve handles structure for GUI
buttonTags = {'button1' 'button2' 'button3'};
if ~isempty(eventdata.OldValue),          % Check for an old selected object
  oldTag = get(eventdata.OldValue,'Tag'),   % Get Tag of old selected object
  index = strcmp(oldTag,buttonTags);     % Find index of match in buttonTags
  set(handles.lineHandles(index),'Visible','off');       % Turn old line off
end
newTag = get(eventdata.NewValue,'Tag'),   % Get Tag of new selected object
index = strcmp(newTag,buttonTags);     % Find index of match in buttonTags
set(handles.lineHandles(index),'Visible','on');         % Turn new line on
guidata(hObject,handles);  % Save handles structure

注意:如果您想更改绘制的三条线中的任何一条,您可以简单地设置其中一个线柄的“XData”和“YData”属性。例如,这会使用新的 x 和 y 数据更新第一条绘制线:

set(handles.lineHandles(1),'XData',xNew,'YData',yNew);

【讨论】:

    【解决方案2】:

    除非您有充分的理由不这样做,否则我认为您应该将绘图代码放在每个单选按钮的回调中。

    不需要做这个大开关场。

    % --- Executes on button press in radiobutton1.
    function radiobutton1_Callback(hObject, eventdata, handles)
    % hObject    handle to radiobutton1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hint: get(hObject,'Value') returns toggle state of radiobutton1
    %%
    %get the values of x y into this callback as you see fit
    plot(x,y)
    

    此外,按钮的“值”已经是单选按钮的两倍。无需像您一样转换它。

    【讨论】:

      猜你喜欢
      • 2012-09-29
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 2018-12-20
      • 2021-05-30
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      相关资源
      最近更新 更多