Sunday, July 30, 2017

[Matlab GUI] Build simple Data Processing tool - 4/5

4. How to add new plot to the existing plot, and Use dynamic legend

After selecting either one variable or two variables, we can both draw a plot and add the legend dynamically on the axes selected.

Example code
-----------------------------------------------------------
% Create a legend name
tleg = handles.legend(1:end-4);
yvar = get(handles.VariablesList,'String');
yvari = get(handles.VariablesList,'Value');
ytemp = yvar(yvari);
% Legend name: filename + variable name
leg = sprintf('%s%s%s',tleg,': ',ytemp{1});
% Get the data of y
result = handles.result;
y = result{yvari,1};

if isequal(checkp,1)% with x-axis variable selected
   % Get the data of x
   xn = get(handles.xVariables,'String');
   xi = get(handles.xVariables,'Value'); xn=xn{xi,1};
   x = result{xi,1};
   % Plot and update the legend dynamically
   pline = plot(handles.axes1,x,y,'DisplayName',leg);
   hl = legend(handles.axes1,'-DynamicLegend');
   xlabel(xn,'interpreter','none');
   ylabel(ytemp{1},'interpreter','none');
else % Without x variable
   % Plot and update the legend dynamically   
   pline = plot(handles.axes1,y,'DisplayName',leg);
   hl = legend(handles.axes1,'-DynamicLegend');
   ylabel(ytemp{1},'interpreter','none');
end
% Set the legend interpreter to none
set(hl,'Interpreter','none');


Related posts
0. Build simple plot tool.
1. Create a tool layout in GUIDE
2. Load mat file in structure array, and Display the file list loaded on the listbox
3. Display variables of mat file, and Plot variables on the axes
4. How to add new plot to the existing plot, and Use dynamic legend
5. Clear the axes, and Move the figure to new figure (Undock)

No comments:

Post a Comment