close all
# graphics_toolkit("gnuplot")
graphics_toolkit("qt")

# !! setting the figure position during the creation is mandatory with gnuplot
# it is impossible to change it afterwards

paper_width = 21;
paper_height = 12;
# margins
paper_margin_left = 2.0;
paper_margin_right = 2.0;
paper_margin_bottom = 2.0;
paper_margin_top = 2.0;
# gaps between plots
inside_gap_vertical = 0.25;
inside_gap_horizontal = 0.25;

paper_position = [0, 0, paper_width, paper_height];
figure(1, "position", paper_position)
clf
set(gcf,'Units','centimeters');
set(gcf,'DefaultAxesUnits', 'centimeters');
set(gcf,'PaperUnits','centimeters');
set(gcf, 'papertype', '<custom>')
set(gcf, "PaperSize", [paper_width paper_height])
set(gcf, 'PaperPositionMode', 'manual');       # turn off 'auto' so it doesn't resize upon printing
#set(gcf, 'PaperPositionMode', 'auto');      # so that the exported figure looks like it does on the screen
set(gcf, "PaperOrientation", "landscape"); # wider is horizontal
set(gcf, 'PaperPosition', paper_position);
set(gcf, "Position", paper_position);

N_rows = 4;
N_cols = 4;

for idx_row = 1:N_rows
  for idx_col = 1:N_cols
    h = axes;

    plot_height = (paper_height - paper_margin_left - paper_margin_right - 
                   inside_gap_vertical / (N_rows - 1)) / N_rows;
    plot_width = (paper_width - paper_margin_bottom - paper_margin_top - 
                  inside_gap_horizontal / (N_cols - 1)) / N_cols;
    position_left = paper_margin_left + (idx_col - 1) * (plot_width + inside_gap_horizontal);
    position_bottom = paper_margin_bottom + (idx_row - 1) * (plot_height + inside_gap_vertical);
    plot(1:10);
    set(h, "Position", [position_left, position_bottom, plot_width, plot_height])

  endfor
endfor

basename = sprintf("multi_v%s_%s", version, graphics_toolkit());
print(sprintf("%s.pdf", basename))
print(sprintf("%s.png", basename))