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