How to return empty vector of figures

18 vues (au cours des 30 derniers jours)
Erwin Werkhoven
Erwin Werkhoven le 4 Juil 2022
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 commentaires
Bruno Luong
Bruno Luong le 4 Juil 2022
MATLAB figure class is
matlab.ui.Figure
Bruno Luong
Bruno Luong le 4 Juil 2022
Modifié(e) : Bruno Luong le 4 Juil 2022
You might even do this:
%if n_plots == 0
figures = []; % instead of figures = Figure.empty
% return
%end

Connectez-vous pour commenter.

Réponses (2)

Rik
Rik le 4 Juil 2022
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 commentaire
Erwin Werkhoven
Erwin Werkhoven le 4 Juil 2022
Bruno Luong's suggestion is simpler than this one and seems to work too. Thanks for thinking along though!

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 4 Juil 2022
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

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by