MATLAB: Change plot in single axes on click (GUIDE)

guideMATLABmultiple plots

Hi, I have a function that generates multiple plots (15 or so). I am able to display all of them (their titles) in a listbox. What I would like to do is to display the plot upon clicking on a proper title in a listbox.
\\This is part of a previous callback
set(handles.figures_list_tag,'string',fieldnames(handles.allfigures));
handles.fieldnames=fieldnames(handles.allfigures);
guidata(hObject,handles)
% --- Executes on selection change in figures_list_tag.
function figures_list_tag_Callback(hObject, eventdata, handles)
selected_figure_index=get(hObject,'Value');
figure_array=cellstr(get(hObject,'String'));
selected_figure=getfield(handles.allfigures,handles.fieldnames{selected_figure_index});
handles.figure_tag;
cla;
hold on
set(handles.figure_tag,'CurrentAxes',findobj(handles.allfigures,selected_figure))
guidata(hObject,handles)
Anyhow, this doesn't work, but just to give you an idea what I have so far. Any advice how to do this? Thanks in advance!

Best Answer

luiv1616 - see the attached code for a quick example. In the GUI OpeningFcn, we create the plots and hide all but the first one
x = -2*pi:0.001:2*pi;
hold(handles.axes1, 'on');
h1 = plot(handles.axes1, x, sin(x), 'r', 'Visible', 'on');
h2 = plot(handles.axes1, x, cos(x), 'b', 'Visible', 'off');
h3 = plot(handles.axes1, x, tan(x), 'g', 'Visible', 'off');
We then update the list box with the plot names
listValues = {'sin', 'cos', 'tan'};
set(handles.listbox1, 'String', listValues');
and save the graphics objects handles to the handles struct
handles.plotHandles = [h1 h2 h3];
guidata(hObject, handles);
(The guidata call is important as it will save the updated handles struct with the new plotHandles member.)
In our list box callback, we just grab the index of the selected list item and set its corresponding graphics object visibility to ON (and turn all others to off)
selectedPlotIndex = get(hObject, 'Value');
set(handles.plotHandles(selectedPlotIndex), 'Visible', 'on');
for k=1:length(handles.plotHandles)
if k ~= selectedPlotIndex
set(handles.plotHandles(k), 'Visible', 'off');
end
end
Try the above and see what happens!
Related Question