Can my ui table also act as my plot legend?

5 views (last 30 days)
I have a GUI with a uitable as well as a plot. Instead of creating a legend, I want to have the line color and marker for the data points to be added in a column to my ui table. Can this be done? This would save space on my GUI, as well as make it easier for displaying data.

Accepted Answer

Joseph Cheng
Joseph Cheng on 29 Jan 2016
yes you absolutely can. It just takes some basic knowledge of HTML and knowing how to get/parse/extract handle properties.
This link http://www.mathworks.com/matlabcentral/answers/92437-how-do-i-change-the-color-of-individual-cells-in-uitable-in-matlab-7-10-r2010a is a good starting point but i added additional information into getting the different parameters in the case that you didn't store them before hand. I'd use the debugger and step through my example to see how to fit your linestyle+marker+color data.
function multi_colored_uitable
%generate random data for example
sampdata = randi(100,10,4);
%get sample plot with sampdata with varying colors and markers for lines
markers = ' -.o';
colors = 'bgrm';
hfig = figure;hax = axes('position',[.1 .55 .8 .4]); hold on
for ind= 1:4
hplot(ind) = plot(sampdata(:,ind),['-' markers(ind) colors(ind)]);
end
% Create a cell array with HTML code for line data
for ind = 1:4
%scale colors to 255 to be converted to hex
linecolor = 255*get(hplot(ind),'Color');
colorinhex = dec2hex(linecolor,2)';
marker = get(hplot(ind),'marker');
if strcmp(marker,'none')
marker=[];
end
linestyle = get(hplot(ind),'Linestyle');
linedata{1,ind} = colText([linestyle marker],['#' colorinhex(:)']);
end
tabledata = [linedata;mat2cell(sampdata,ones(1,10),ones(4,1))];
% Define the column names
colnames = {'first', 'second', 'third', 'fourth'};
uitable(hfig, 'Data', tabledata, ...
'ColumnName', colnames, ...
'Units', 'normalized', ...
'Position', [.1 .05 .8 .4]);
end
function outHtml = colText(inText, inColor)
% return a HTML string with colored font
outHtml = ['<html><font color="',...
inColor, ...
'">', ...
inText, ...
'</font></html>'];
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!