Changing font size of all axes labels

2.022 views (last 30 days)
Morten Nissov
Morten Nissov on 20 Nov 2019
Commented: Adam Danz on 28 Aug 2021
I often need to make pretty cumbersome plotting definitions in MATLAB, an example of which can be seen below
hold on
line([0,N], [R(1,1),R(1,1)],...
hold off
grid on
legend({'closed loop','setpoint'},...
For simplicities sake I have only included one of four subplots. In these plots I end up writing
quite a lot. I am asking if there is a better way to do this given that the font size and interpreter type is the same for my xlabel, ylabel, and legend which it very often is for me.
I have seen some pages recommending I use something along the lines of
But this doesnt work for the labels for me.
TLDR: What is a "good" coding style way of configuring the tedious plot options like font size and interpreter en masse.
  1 Comment
Adam Danz
Adam Danz on 20 Nov 2019
I wonder what's not working for the label font size when axis font size is set using set(gca,'fontsize',14)

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 20 Nov 2019
Edited: Adam Danz on 20 Nov 2019
Idea 1: put all shared name-value properties into a cell array
For sets of name-value pairs that will be assigned to several objects, you can group them into 1 variable and assign them like this.
extraInputs = {'interpreter','latex','fontsize',14}; % name, value pairs
legend({'closed loop','setpoint'},extraInputs{:})
Idea 2: set the axis properties when possible
Axes do not have an interpreter property but you could avoid assigning font size (and other properties) to each axis label and legend by assigning those properties to the axes.
legend({'closed loop','setpoint'},'interpreter','latex')
Idea 3: create a local function designed to produce formatted axes and labels
Lastly, if you're creating a bunch of subplots that all have the same set of properties, create a local function that creates subplots and assigns the formatted axis labels.
ax = newsubplot(221,'k','h_1');
line([0,1], [1,2],'color','red','linestyle','--','LineWidth',2)
legend({'closed loop','setpoint'},'interpreter','latex')
function ax = newsubplot(position, xlab, ylab)
% Creates new subplot in specified position on current figure
% with xlab xlabel and ylab ylabel
ax = subplot(position);
hold on
set(ax,'FontSize',14) %and other properties
grid on
Adam Danz
Adam Danz on 28 Aug 2021
Use findobj to find all objects in a figure that have a font size property and then use the set command to set the font size to all of those objects. If you have trouble implementing that I can show you how.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by