MATLAB: How to plot multiple lines in the same graphics using GUI

handle graphicsMATLABmultiple_plot

I was trying to plot a graphics of 'c vs X' for different 't'(t1,t2,t3 etc) in the same graph using GUI. Note that, lines for different t should be plotted one by one (i.e after plot of line 1 (t1), it will wait for input of t2 to plot line 2 and so on).Is there anybody to help in doing that? Thanks in advance !
MY CODE IS:
function varargout = gui_trail(varargin)
% GUI_TRAIL MATLAB code for gui_trail.fig
% GUI_TRAIL, by itself, creates a new GUI_TRAIL or raises the existing
% singleton*.
%




% H = GUI_TRAIL returns the handle to a new GUI_TRAIL or the handle to
% the existing singleton*.
%
% GUI_TRAIL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_TRAIL.M with the given input arguments.
%
% GUI_TRAIL('Property','Value',...) creates a new GUI_TRAIL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui_trail_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui_trail_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help gui_trail
% Last Modified by GUIDE v2.5 08-Feb-2011 04:15:12
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_trail_OpeningFcn, ...
'gui_OutputFcn', @gui_trail_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before gui_trail is made visible.
function gui_trail_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB






% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to gui_trail (see VARARGIN)
% Choose default command line output for gui_trail
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_trail wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_trail_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)



% Get default command line output from handles structure
varargout{1} = handles.output;
function value_x_Callback(hObject, eventdata, handles)
% hObject handle to value_x (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of value_x as text
% str2double(get(hObject,'String')) returns contents of value_x as a double
% --- Executes during object creation, after setting all properties.

function value_x_CreateFcn(hObject, eventdata, handles)
% hObject handle to value_x (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

% See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function value_t_Callback(hObject, eventdata, handles)
% hObject handle to value_t (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of value_t as text
% str2double(get(hObject,'String')) returns contents of value_t as a double
% --- Executes during object creation, after setting all properties.
function value_t_CreateFcn(hObject, eventdata, handles)
% hObject handle to value_t (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in draw.
function draw_Callback(hObject, eventdata, handles)
% hObject handle to draw (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%CODE TO EXECUTE PUSH BUTTON %%%
another='Yes';
while strcmp(another,'Yes')
x=str2double(get(handles.value_x,'String'));
t=str2double(get(handles.value_t,'String'));
x=1:x;
c=t*exp(-0.05*x);
figure;
plot(x,c(x));
qstring1='Another calculation?';
another=questdlg(qstring1,'Another calculation?','Yes','No','No');
if strcmp(another,'Yes')
qstring='Draw on same graph?';
dsameG=questdlg(qstring,'Draw on same graph?','Yes','No','No');
else
dsameG='No';
end
if strcmp(dsameG,'Yes')
hold on;
end
end
N.B. Sorry for so long submission !

Best Answer

Your draw callback always creates a new figure and then plots in to that. You should instead do something like
ah = findobj(0,'Type','axes','Tag','MyPlotAxes');
if isempty(ah)
fh = figure;
ah = axes('Parent',fh, 'Tag', 'MyPlotAxes');
hold(ah, 'on');
end
plot(ah, x, c(x));
Related Question