1

我正在尝试创建一个 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
4

2 回答 2

1

您似乎正在尝试以类似于blinkdagger.com 上的示例教程的方式创建单选按钮面板。具体来说,我相信您正在尝试创建一个SelectionChangeFcn来定义单选按钮如何修改您的 GUI。我建议如下:

首先,不是每次选择单选按钮时都重新绘制一条线,我建议您在创建 GUI 时绘制所有线,然后将线的“可见”属性调整为“开”或“关”取决于选择了哪个按钮。当您制作 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);
于 2009-07-21T01:42:00.853 回答
0

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

不需要做这个大开关场。

% --- 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)

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

于 2009-07-20T20:45:49.933 回答