使用单选按钮面板选择不同的绘图选项

发布于 2024-07-27 01:17:14 字数 772 浏览 1 评论 0原文

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

I am trying to create a GUI that will take several inputs and run them through several functions. I wish to use a radio button panel to switch between different graphs, but i just cant seem to get it working. Here is a sample of my code.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2024-08-03 01:17:14

您似乎正在尝试创建单选按钮面板 以类似于blinkdagger.com 上的示例教程。 具体来说,我相信您正在尝试创建 SelectionChangeFcn< /a> 定义单选按钮如何修改 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);

It appears that you are trying to create a radio button panel in a way similar to this example tutorial on blinkdagger.com. Specifically, I believe you are trying to create a SelectionChangeFcn to define how the radio buttons modify your GUI. I would suggest the following:

First, instead of replotting a line every time a radio button is selected, I would suggest that you plot all of your lines when you create your GUI and then adjust the 'Visible' property of the lines to either 'on' or 'off' depending on which button is selected. When you make your GUI, you can add these lines somewhere in your code (after the axes is created and placed in the handles variable):

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

This will plot three sets of lines on the same axes. These lines are initially not visible, and handles to each plotted line are collected in a vector variable lineHandles. The last two lines above add the line handles to the handles structure and update the GUI data (hObject should be a handle to the GUI figure window!).

Now, you can use the following for your 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

NOTE: If you ever want to change any of the three lines that are plotted, you can simply set the 'XData' and 'YData' properties of one of the line handles. As an example, this updates the first plotted line with new x and y data:

set(handles.lineHandles(1),'XData',xNew,'YData',yNew);
那片花海 2024-08-03 01:17:14

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

不需要做这么大的开关站。

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

此外,从按钮中出来的“值”已经是单选按钮的双精度值。 无需像您所做的那样对其进行转换。

Unless you have a good reason to do otherwise, I think you should put the plotting code inside the callback for each radio button.

No need to do this big switchyard.

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

Also, the 'value' that comes out of the button it is already a double for radio buttons. No need to convert it as you are doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文