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
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
2022년 7월 4일
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
2022년 7월 4일
Bruno Luong
2022년 7월 4일
MATLAB figure class is
matlab.ui.Figure
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개)
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
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')
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!