How to return empty vector of figures
18 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
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
Réponses (2)
Rik
le 4 Juil 2022
plot_configs=[];
figures = draw_plots(plot_configs)
plot_configs=ones(1,2);
figures = draw_plots(plot_configs)
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
Steven Lord
le 4 Juil 2022
Preallocate the array using gobjects.
F = gobjects(1, 3)
F(2) = figure;
F
To check if an element has been filled in, ask if it is a matlab.ui.Figure.
isa(F(3), 'matlab.ui.Figure') % false
isa(F(2), 'matlab.ui.Figure') % true
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
isa(F, 'matlab.ui.Figure')
0 commentaires
Voir également
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!