Clear Filters
Clear Filters

Create a stand-alone legend for tabulated data

9 views (last 30 days)
I'm analyizing old datasets for work. As part of this work I am grouping the data into bins as shown in the table. I am then creating PDF reports of the data, and as part of that pdf creation I need a stand alone legend to insert into its own area. Historically I had done a continuous color scale, and was easily able to produce a stand alone colorbar, but the powers who be would like discrete data groupings.
I am struggling with producing figure handles in a loop that then can be used to produce a stand alone legend. All attempts are failing. The general code is this (I've stripped all my attempts out of the the legend producer):
dat_bins=readtable("Bin_Input.xlsx");
%create RGB's for dat_bins
for j=1:length(dat_bins.bin_color)
if matches(string(dat_bins.bin_color(j)),"b")
dat_bins.rgb{j}=[0 0 1];
elseif matches(string(dat_bins.bin_color(j)),"g")
dat_bins.rgb{j}=[0 1 0];
elseif matches(string(dat_bins.bin_color(j)),"y")
dat_bins.rgb{j}=[1 1 0];
elseif matches(string(dat_bins.bin_color(j)),"o")
dat_bins.rgb{j}=[1 165/255 0];
else
dat_bins.rgb{j}=[1 0 0];
end
end
fig_leg=figure('Visible', 'on'); %create stand alone legend
axis off
for k=1:length(dat_bins.bin_start)
fig_leg(k) = plot(nan, nan, 'color', dat_bins.rgb{k});
end
legend(fig_leg, {string(dat_bins.bin_name)'})
Any help would be greatly appreciated.

Accepted Answer

oran
oran on 7 Jun 2023
Figured it out. Partially borrowed from: here
dat_bins=readtable("Bin_Input.xlsx");
%create RGB's for dat_bins
for j=1:length(dat_bins.bin_color)
if matches(string(dat_bins.bin_color(j)),"b")
dat_bins.rgb{j}=[0 0 1];
elseif matches(string(dat_bins.bin_color(j)),"g")
dat_bins.rgb{j}=[0 1 0];
elseif matches(string(dat_bins.bin_color(j)),"y")
dat_bins.rgb{j}=[1 1 0];
elseif matches(string(dat_bins.bin_color(j)),"o")
dat_bins.rgb{j}=[1 165/255 0];
else
dat_bins.rgb{j}=[1 0 0];
end
end
fig_leg=figure('Visible', 'on'); %create stand alone legend
axis off
for k=1:length(dat_bins.bin_start)
fig_leg=scatter(nan,nan, 'MarkerFaceColor', dat_bins.rgb{k});
hold on
end
legend(dat_bins.bin_name')
% legend window
set(gcf,'Position',[0,0,1024,1024]);
legend_handle = legend('Orientation','vertical');
% This sets the figure to have the same size as the legend
set(gcf,'Position',(get(legend_handle,'Position')...
.*[0, 0, 1, 1].*get(gcf,'Position')));
set(legend_handle,'Position',[0,0,1,1]);
% Put the figure back in the middle screen area
set(gcf, 'Position', get(gcf,'Position') + [500, 400, 0, 0]);

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!