How to Permanently Change Default Title Options in tiledlayout?

46 vues (au cours des 30 derniers jours)
Massimiliano
Massimiliano le 27 Nov 2024 à 15:25
Réponse apportée : Ruchika Parag le 28 Nov 2024 à 9:31
Hi everyone,
I recently switched from using subplot to tiledlayout for creating multiple plots, as I find the latter more adaptable and easier to work with. However, I encountered an issue when adding titles to my plots: the title function in tiledlayout does not seem to adhere to the general graphics default options that I have modified.
Since I frequently generate several plots, I’m wondering if there is a way to permanently set default properties for the titles (and potentially other elements) when using tiledlayout. In particular, I'm would like that the interpreter is set to 'latex'. For now, I’ve created a custom function (code below) to manually apply the desired options, and I'm wondering if you know how to adapt in order to modify permanently also the options of tiledlayout.
function [] = fplot_DefaultOption(opt)
% Defines the default settings for plots.
%
% To view the possible settings, use the command:
% clc; get(groot, 'factory')
arguments
opt.interpreter = 'latex'
% Selects the interpreter. Possible values include 'latex' or 'tex'.
opt.fontsize_axis = 8;
opt.fontsize_label = 10;
opt.fontsize_title = 13;
opt.fontsize_legend = 8;
% opt.fontsize_axis = 8;
% opt.fontsize_label = 9;
% opt.fontsize_title = 13;
% opt.fontsize_legend = 8;
opt.posX = [0.4, 1];
opt.posY = [0.075, 0.875];
% Selects the position of the plot on the screen. `posX` defines the
% position along the horizontal axis, while `posY` defines the
% position along the vertical axis. Both are vectors that indicate
% the start and end points as percentages (for example, if
% posX = [0.1, 0.9], the image will start at 10% and end at 90%
% of the screen width along the horizontal axis).
end
% Set Latex as interpreter
set(groot, 'defaultAxesTickLabelInterpreter', opt.interpreter);
set(groot, 'defaultLegendInterpreter', opt.interpreter);
set(groot, 'defaultTextInterpreter', opt.interpreter);
set(groot, 'defaultGraphplotInterpreter', opt.interpreter);
% Set font size
set(groot, 'defaultAxesFontSize', opt.fontsize_axis);
set(groot, 'defaultAxesLabelFontSizeMultiplier', opt.fontsize_label/opt.fontsize_axis);
set(groot, 'defaultAxesTitleFontSizeMultiplier', opt.fontsize_title/opt.fontsize_axis);
set(groot, 'defaultLegendFontSizeMode', 'manual');
set(groot, 'defaultLegendFontSize', opt.fontsize_legend);
% Set title position
set(groot, 'defaultAxesTitleHorizontalAlignment', 'left');
% Set figure position
set(groot, 'defaultFigureUnits', 'normalized')
pos = [opt.posX(1) opt.posY(1) opt.posX(2)-opt.posX(1) opt.posY(2)-opt.posY(1)];
set(groot, 'defaultFigurePosition', pos)
end

Réponses (1)

Ruchika Parag
Ruchika Parag le 28 Nov 2024 à 9:31
HI @Massimiliano, to set default properties for titles and other elements in MATLAB's 'tiledlayout', you can modify your custom function to include the necessary settings for the 'tiledlayout' object. Unfortunately, MATLAB does not directly support setting defaults for 'tiledlayout' properties via 'groot', but you can create a utility function to apply your preferred settings whenever you create a new tiled layout.
Here's how you can adapt your existing function to also handle 'tiledlayout':
function [] = fplot_DefaultOption(opt)
% Defines the default settings for plots.
arguments
opt.interpreter = 'latex'
opt.fontsize_axis = 8;
opt.fontsize_label = 10;
opt.fontsize_title = 13;
opt.fontsize_legend = 8;
opt.posX = [0.4, 1];
opt.posY = [0.075, 0.875];
end
% Set Latex as interpreter
set(groot, 'defaultAxesTickLabelInterpreter', opt.interpreter);
set(groot, 'defaultLegendInterpreter', opt.interpreter);
set(groot, 'defaultTextInterpreter', opt.interpreter);
set(groot, 'defaultGraphplotInterpreter', opt.interpreter);
% Set font size
set(groot, 'defaultAxesFontSize', opt.fontsize_axis);
set(groot, 'defaultAxesLabelFontSizeMultiplier', opt.fontsize_label / opt.fontsize_axis);
set(groot, 'defaultAxesTitleFontSizeMultiplier', opt.fontsize_title / opt.fontsize_axis);
set(groot, 'defaultLegendFontSizeMode', 'manual');
set(groot, 'defaultLegendFontSize', opt.fontsize_legend);
% Set title position
set(groot, 'defaultAxesTitleHorizontalAlignment', 'left');
% Set figure position
set(groot, 'defaultFigureUnits', 'normalized');
pos = [opt.posX(1), opt.posY(1), opt.posX(2) - opt.posX(1), opt.posY(2) - opt.posY(1)];
set(groot, 'defaultFigurePosition', pos);
% Custom function to apply default options to tiled layout
function applyTiledLayoutDefaults(t)
% Apply interpreter to all titles in the tiled layout
titleHandle = title(t, ' ');
set(titleHandle, 'Interpreter', opt.interpreter, 'FontSize', opt.fontsize_title);
end
% Nested function to create a tiled layout with default options
function t = createTiledLayout(varargin)
t = tiledlayout(varargin{:});
applyTiledLayoutDefaults(t);
end
% Assign the nested function to the base workspace for easy access
assignin('base', 'createTiledLayout', @createTiledLayout);
end
After creating a function, do the following steps:
  1. Run the Function: Call 'fplot_DefaultOption' to set your preferences. This will also create a 'createTiledLayout' function in the base workspace.
  2. Create Tiled Layouts: Use the 'createTiledLayout' function instead of 'tiledlayout' to create your layouts with defaults applied.
% Example usage
fplot_DefaultOption(); % Set defaults
% Create a tiled layout with default settings
t = createTiledLayout(2, 2); % 2x2 tiled layout
% Add plots
nexttile;
plot(rand(10, 1));
title('Plot 1');
nexttile;
plot(rand(10, 1));
title('Plot 2');
This method makes your workflow more efficient by automatically applying your preferred settings to new tiled layouts. You can customize the `applyTiledLayoutDefaults` function to include more default settings if needed.

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by