How to return empty vector of figures
21 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
2022-7-4
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
2022-7-4
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!