MATLAB: Problem with plotting

legendplotting

I have two problems with plotting.
1. 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).Now I want to add legends for each line (say, t1, t2, t3 etc). How can I do that?
2. When I run the program for first line(t1) it is fine but for second plot (t2) the GUI showed plot axes with label and title. How can I get rid off it?
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 09-Mar-2011 03:34:25
% 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)
x=str2double(get(handles.value_x,'String'));
t=str2double(get(handles.value_t,'String'));
x=1:x;
c=t*exp(-0.05*x);
ah = findobj(0,'Type','axes','Tag','MyPlotAxes');
if isempty(ah)
fh = figure;
ah = axes('Parent',fh, 'Tag', 'MyPlotAxes');
hold(ah, 'all');
end
plot(ah, x, c(x));
title('concentration vs Y');
grid on;
xlabel ('Distance in Y-direction (m)');
ylabel('Concentration');

Best Answer

For the first question, use the LEGEND command.
L = length(get(axeshandle,'children'));
legend([repmat('t',L,1),sprintf('%i',1:L).']))
For the second question, you are calling the TITLE function and YLABEL etc, why do you call them if you do not want those things?
Related Question