views:

1837

answers:

2

Hi,

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
A: 

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.

MatlabDoug
+1  A: 

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);
gnovice

related questions