Flavian Hautbois
Flavian Hautbois

Reputation: 3070

Handling multiple plots in MATLAB

I am using plot(X) whereX is an n-by-k matrix, which produces k plots with n points.

How do I show the legend for this plot? More importantly, is there an easy way to show checkboxes to show or not show certain plots?

Upvotes: 2

Views: 474

Answers (2)

slitvinov
slitvinov

Reputation: 5768

I think you can find this section of Documentation useful.
GUI that Displays and Graphs Tabular Data
http://www.mathworks.com/help/techdoc/creating_guis/bropmbk-1.html

Please use this body for plot_callback function in tableplot.m file to get a dirty implementation of the flexible legend.

function plot_callback(hObject, eventdata, column)
% hObject     Handle to Plot menu
% eventdata   Not used
% column      Number of column to plot or clear

colors = {'b','m','r'}; % Use consistent color for lines
colnames = get(htable, 'ColumnName');
colname = colnames{column};
lgidx = get(haxes, 'UserData');
if isempty(lgidx)
   lgidx = false(size(colnames));
end

if get(hObject, 'Value')
    % Turn off the advisory text; it never comes back
    set(hprompt, 'Visible', 'off')
    % Obtain the data for that column
    ydata = get(htable, 'Data');
    set(haxes, 'NextPlot', 'Add')
    % Draw the line plot for column
    hplot = plot(haxes, ydata(:,column),...
             'DisplayName', colname,...
             'Color', colors{column});
    lgidx(column) = true;
else % Adding a line to the plot
    % Find the lineseries object and delete it
  hplot = findobj(haxes, 'DisplayName', colname);
  lgidx(column) = false;
  delete(hplot);
end
if any(lgidx)
  legend(haxes, colnames{lgidx} );
else
  legend(haxes, 'off')
end
  set(haxes, 'UserData', lgidx);
end

Upvotes: 1

Amro
Amro

Reputation: 124573

An example:

x = cumsum(rand(100,3)-0.5);         %# three series with 100 points each
h = plot(x);
legend(h, {'first' 'second' 'third'})

screenshot

Upvotes: 1

Related Questions