gaurav
gaurav

Reputation: 1

unable to plot on axes in matlab gui from listbox

I am working on MATLAB GUI in which i am updating the work-space variables in a list box and then trying to plot them on axes in GUI.

I have one other push button for performing plotting operation. But when i click on plot button, i get plots in a figure which pops up.

But according to my application i have to create the plots in axes. I am unable to do so

Kindly help

MY plot button code is as follows:

function plot_button_Callback(hObject, eventdata, handles, varargin)
% hObject    handle to plot_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[x] = get_var_names(handles);
if isempty(x) 
    return
end
if isequal(x,'a')
% figure(gcf)

 try
    figure(1)

    evalin('base',['plot(a,b,''--r'')'])
    hold all
    evalin('base',['plot(a,c,''k'')'])
    hold all
    evalin('base',['plot(a,d,''g'')'])
    figure(2)
    evalin('base',['plot(a,e,''g'')'])
    hold all
       grid on   
     catch ex
    errordlg(...
      ex.getReport('basic'),'Error generating linear plot','modal')
 end

Upvotes: 0

Views: 518

Answers (1)

Trogdor
Trogdor

Reputation: 1346

Within each GUI callback, you have a variable called handles which is the key to editing/accessing any item in your GUI. In the case of plotting to an existing axis, you need to add an additional argument to the plot function. Here's a line of code I yanked from a GUI that I wrote:

plot(handles.axes1, xdata, ydata);

Now, this approach might not work easily for you because you are using the evalin function (which I don't recommend doing, it'd be much better to pass that information in to the gui). Regardless, a good way to implement your goal with these constraints is

a = evalin('base','a');
b = evalin('base','b');
plot(handles.axes1,a,b,'--r');

Your GUI axes might not be named axes, you'll have to check on that. You should also probably remove the figure(1) call, if I understand your goal correctly.

Also, you don't need to invoke hold all after each time you plot, once is sufficient.

Upvotes: 0

Related Questions