How to return empty vector of figures

Hi all,
I have a function draw_plots with an input vector plot_configs that should return a vector of figures. If the plot_configs vector is empty it should return an empty figure vector. But with the code underneath I get this error:
Output argument "figures" (and possibly others) not assigned a value in the execution with "draw_plots" function.
That makes perfect sense. So I tried to initialize figures with figures = Figure.empty but that doesn't work either:
Unable to resolve the name 'Figure.empty'.
So how to initialize/return an empty vector of figures? Thanks in advance!
Please note that we're not interested in showing the figures, just saving them to disk.
function figures = draw_plots(plot_configs, tsc, masks, n_rows, n_cols)
plot_configs = plot_configs([plot_configs.enable]); % filter enabled plot configs
color_order = [
0.0 0.0 0.8;
0.0 0.5 0.0;
0.8 0.0 0.0;
0.0 0.8 0.8;
0.8 0.0 0.8;
0.8 0.8 0.0;
0.2 0.2 0.2];
n_plots = length(plot_configs);
n_plots_per_page = n_rows * n_cols;
% figures = Figure.empty; <-- How I hoped to fix the error
for pc_idx = 1:n_plots
plot_idx = mod(pc_idx, n_plots_per_page);
if (plot_idx == 0) % Fill up figure with subplots until max number of plots per page
plot_idx = n_plots_per_page;
elseif (plot_idx == 1)
fig = figure('defaultAxesColorOrder', color_order, 'visible', 'off');
inner_w = 1200;
inner_h = 400 * n_rows;
fig.InnerPosition(3) = inner_w;
fig.InnerPosition(4) = inner_h;
fig_idx = ceil(pc_idx / n_plots_per_page);
figures(fig_idx) = fig;
end
sub = subplot(n_rows, n_cols, plot_idx);
hold on;
box on;
grid on;
pc = plot_configs(pc_idx);
mask = masks.get(pc.filter);
data_x = pc.signal_x.getter(tsc);
data_x = data_x(mask);
if (strcmp(pc.signal_x.generic_name, 'Time'))
data_x = datetime(data_x, 'ConvertFrom', 'datenum', 'Format', 'yyyy-MM-dd HH:mm:ss');
end
for signal_y = pc.signals_y
data_y = signal_y.getter(tsc);
data_y = data_y(mask);
plot = scatter(sub, data_x, data_y, 1, 'filled');
fontsize(plot, 8, "pixels")
end
title_str = pc.title;
xlabel_str = pc.signal_x.generic_name + ' [' + pc.unit_x + ']';
ylabel_str = pc.unit_y;
title(title_str, 'Interpreter', 'none');
xlabel(xlabel_str, 'Interpreter', 'none');
ylabel(ylabel_str, 'Interpreter', 'none');
end
end

댓글 수: 6

Garmit Pant
Garmit Pant 2022년 7월 4일
Can you please provide the complete code to reproduce this and use the 'Insert Code' option to write your code in the comments?
Erwin Werkhoven
Erwin Werkhoven 2022년 7월 4일
Ok I updated the code sample, but I'm not sure if the problem becomes more clear this way... The trick is that we want for instance 8 subplots in a figure, and then continue filling up the next figure.
Bruno Luong
Bruno Luong 2022년 7월 4일
편집: Bruno Luong 2022년 7월 4일
May be you don''t need at all cost return an empty array of figure. You could initialized
figures = [];
in your function as default value and returns it when nplots == 0.
That may be sufficient to deal it outside.
Remember that matlab can deal figure handle array as array of figure numbers, which is standard numerical array. This is for compatibility with HG1.
Otherwise follow @Rik answer that create a dummy figure with 'visible' off, close it ad use the handle as template to create an empty figure array. This is a little cumbersome though for a small variable creation.
Erwin Werkhoven
Erwin Werkhoven 2022년 7월 4일
Ok I did
if n_plots == 0
figures = [];
return
end
% other code
That seems to work. Thank you!
Just for the sake of curiosity, is there no Figure class since you cannot call figures = Figure.empty?
MATLAB figure class is
matlab.ui.Figure
Bruno Luong
Bruno Luong 2022년 7월 4일
편집: Bruno Luong 2022년 7월 4일
You might even do this:
%if n_plots == 0
figures = []; % instead of figures = Figure.empty
% return
%end

댓글을 달려면 로그인하십시오.

답변 (2개)

Rik
Rik 2022년 7월 4일

0 개 추천

plot_configs=[];
figures = draw_plots(plot_configs)
figures =
1×0 empty Figure array.
plot_configs=ones(1,2);
figures = draw_plots(plot_configs)
figures =
1×2 Figure array: Figure Figure
function figures = draw_plots(plot_configs, varargin)
n_plots=numel(plot_configs);
if n_plots==0
figures=figure('visible', 'off');
close(figures)
figures(1)=[];
return
end
for n=1:n_plots
fig = figure(varargin{:}, 'visible', 'off');
figures(n) = fig;
end
end

댓글 수: 1

Erwin Werkhoven
Erwin Werkhoven 2022년 7월 4일
Bruno Luong's suggestion is simpler than this one and seems to work too. Thanks for thinking along though!

댓글을 달려면 로그인하십시오.

Steven Lord
Steven Lord 2022년 7월 4일

0 개 추천

Preallocate the array using gobjects.
F = gobjects(1, 3)
F =
1×3 GraphicsPlaceholder array: GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder
F(2) = figure;
F
F =
1×3 graphics array: GraphicsPlaceholder Figure GraphicsPlaceholder
To check if an element has been filled in, ask if it is a matlab.ui.Figure.
isa(F(3), 'matlab.ui.Figure') % false
ans = logical
0
isa(F(2), 'matlab.ui.Figure') % true
ans = logical
1
Or if they've all been filled in you can ask if the array as a whole is a matlab.ui.Figure.
F(3) = figure;
F(1) = figure;
F
F =
1×3 Figure array: Figure Figure Figure
isa(F, 'matlab.ui.Figure')
ans = logical
1

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

질문:

2022년 7월 4일

답변:

2022년 7월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by