Changing Fonts Size in Matlab Plots - matlab

I want to change Font Size for xlabel, ylabel, axis size, legend font size a.k.a everything at once, is this possible? By default, font is Helvetica 10.
Is there way to change this?
I want to use 'FontSize',14, for x or y labels.

Jonas's answer is good, but I had to modify it slightly to get every piece of text on the screen to change:
set(gca,'FontSize',30,'fontWeight','bold')
set(findall(gcf,'type','text'),'FontSize',30,'fontWeight','bold')

If you want to change font size for all the text in a figure, you can use findall to find all text handles, after which it's easy:
figureHandle = gcf;
%# make all text in the figure to size 14 and bold
set(findall(figureHandle,'type','text'),'fontSize',14,'fontWeight','bold')

It's possible to change default fonts, both for the axes and for other text, by adding the following lines to the startup.m file.
% Change default axes fonts.
set(0,'DefaultAxesFontName', 'Times New Roman')
set(0,'DefaultAxesFontSize', 14)
% Change default text fonts.
set(0,'DefaultTextFontname', 'Times New Roman')
set(0,'DefaultTextFontSize', 14)
If you don't know if you have a startup.m file, run
which startup
to find its location. If Matlab says there isn't one, run
userpath
to know where it should be placed.

If anyone was wondering how to change the font sizes without messing around with the Matlab default fonts, and change every font in a figure, I found this thread where suggests this:
set(findall(fig, '-property', 'FontSize'), 'FontSize', 10, 'fontWeight', 'bold')
findall is a pretty handy command and in the case above it really finds all the children who have a 'FontSize' property: axes lables, axes titles, pushbuttons, etc.
Hope it helps.

To change the title font size, use the following example
title('mytitle','FontSize',12);
to the change the graph axes label font size, do the following
axes('FontSize',24);

Jonas's answer does not change the font size of the axes.
Sergeyf's answer does not work when there are multiple subplots.
Here is a modification of their answers that works for me when I have multiple subplots:
set(findall(gcf,'type','axes'),'fontsize',30)
set(findall(gcf,'type','text'),'fontSize',30)

To change the default property for your entire MATLAB session, see the documentation on how default properties are handled.
As an example:
set(0,'DefaultAxesFontSize',22)
x=1:200; y=sin(x);
plot(x,y)
title('hello'); xlabel('x'); ylabel('sin(x)')

Related

How to change Font Size in Simulink Scope?

I'm trying to change font size of axes, legend and title of a scope used in simulink. I've seen lots of answers for changing font size in a plot made from workspace, like using 'setgca' and 'fontsize' property, but couldn't find anything about changing font size within a simulink scope.
I have tried couple methods, but there's no direct way to implement this. However, we could adjust the font size of titles and x/y labels inside the Figure associated with scope
Directing to scope to figure
After you run the scope, click the scope. In the task bar, select
File > Print to Figure
This will bring you to a figure where we can edit figure's fonts.
Edit font inside the figure
Edit > Figure Properties
In the pop-up window, edit the font.
In simulink is possible to change the font style and size using going to:
Diagram > Format > Font Styles for Model
There you can change the font style and size for blocks, lines and annotations.
There isn't any functionality to change these from a pulldown menu, however, they can all be changed using code.
The primary thing to note is that a Simulink Scope is just a MATLAB Figure Window in disguise, and hence it can be manipulated using standard Handle Graphics commands once you have the handle to the scope block you want to manipulate.
For instance to change the size of the legend you'd do:
% Get the Name of the block you want to change
scope_name = get_param(gcb,'Name');
% Get the handle of the figure window used for the scope
hs = findall(0,'Tag','SIMULINK_SIMSCOPE_FIGURE','Name',scope_name);
% Get the handle to the axes on the scope
% (For simplicity, here we'll assume there is only one axis on the scope.
% If there are multiple axes, then you'll need to select which one to manipulate.)
ha = findall(hs,'Type','Axes');
% Get the handle to the legend
hl = get(ha,'Legend');
% Change the font size
set(hl,'FontSize',12);
Given any of the above handles you can manipulate it using set and get just like any Handle Graphics object.
I am not sure how to access the scope object from MATLAB, however, I managed to change the legend and titles text sizes by simply resizing the scope window. I know it's not exactly the right way to do this but it works.

Change legend line style

I am playing with the visual effect of plots, and a question came up while changing the style of a legend.
To be able to save the figure with legends big enough that can be seen usually one needs to change the FontSize property to e.g. 24. When you do that, the size of the font changes, however, the small line next to it has the same size than when it was small. The proportion between line/text seem quite appropriate to me with a FontSize of around 10, while I believe that with big font sizes bigger "eat" visually the line, which is the important part.
Example with fontsize 30 and 10 (please ignore how much I suck in mspaint and the low resolution of the zoomed legend). The proportion between line/text is nicer in the small one.
I was wondering if there is a way to modify that line. I have been checking the properties but I haven't found any relevant one.
NOTE: The LineWidth property does not change the width of the colour lines, but the width of the bounding box.
You could play with the outputs arguments of legend, especially the icons variable (check here).
According to the docs, they correspond to
Objects used to create the legend icons and descriptions, returned as
text, patch, and line object.
Therefore you might use something like this to modify the LineWidth property of any of your plot, or both of course:
clear
clc
close all
x = 1:10;
plot(x,rand(1,10));
hold on;
plot(x,x,'k');
[h,icons,plots,str] = legend('First plot','Second plot','Location','NorthWest');
set(h,'FontSize',30);
set(icons(:),'LineWidth',2); %// Or whatever
Which outputs:
Note that I used R2014a so it might be a bit different for R2014b.

Change the Font size of the legend in matlab

I have a figure with a legend in Matlab, I want to know how to change the fontsize of the legend (ie: increase the size of the words "speed" and "distance")
plot(x1,y1,x2,y2)
legend('speed','distance')
If you had searched a bit, you would have found it:
From the documentation of Mathworks
figure
plot(x,ydata,'*',x,ycos)
legend({'Collected Data','y = cos(x)'},'FontSize',8,'FontWeight','bold')
It's pretty clear I think.

Matlab: Scale figures for publishing - exact dimensions and font sizes

I am currently writing up a scientific thesis and am very desparate about creating figures that have the exact dimensions I want them to have. Especially the font sizes do not match.
I already googled alot and there are a bunch of guides and scripts about this topic but nothing really helped - I have not yet figured out (sorry) why my approach does not work:
FS=8; %font size in points (the same as in my document)
width=12; %width of figure in cm
height=4; %height of figure in cm
scatter(1:20,rand(20,1));
xlabel('X','fontsize',FS),ylabel('Y','fontsize',FS),title('X vs. Y','fontsize',FS)
%now I scale the figure and place it in the bottom left corner. The white margins around it are cropped automatically
set(gca,'units','centimeters','outerposition',[0 0 width height])
%export as .eps
print(gcf,'-depsc','test')
When I load test.eps into Inkscape, the figure is 10.16 x 3.529 cm large and the font sizes (of title and axis labels) are 10.
How do I get a figure with the exact scaling, especially regarding the font size?
I did the following:
FS=8; %font size in points (the same as in my document)
width=12; %width of figure in cm
height=4; %height of figure in cm
scatter(1:20,rand(20,1));
set(gca, 'fontsize', FS);
xlabel('X','fontsize',FS),ylabel('Y','fontsize',FS),title('X vs. Y','fontsize',FS)
set(gcf,'units','centimeters','position',[0 0 width height])
export_fig(gcf, 'test.pdf', '-transparent', '-nocrop')
The output figure is 12cm x 4cm. The font size still claims to be 10 in Inkscape, however, but it looks the same size as that in the figure. Export_fig can be downloaded from the MATLAB file exchange.
Here is how I solve it as of now - it is not exactly elegant but it works...
I plot my figure and arrange and scale it in the figure window the way I want it to be scaled:
set(gcf,'units','centimeters','position',[0 0 width height])
Due to the white margins around the axes, I increase the width/height by approximate (trial and error...) values that the margins use up. I then export it:
export_fig(gcf,'test','-eps','-transparent')
And load it into Inkscape. Now I set the document properties so that the document has the exact size I want my figure to have - the figure is partly out of this frame because I increased the width/height earlier.
Then I arrange the axes to have as much white space between them as I want them to have; hopefully, everything is inside the document borders after that.
Probably the drawing is smaller than the document borders now - to ensure that it will not get expanded, messing up the scaling, when I put it in my actual document (my thesis, not the Inkscape document...), I simply create a white background that matches the document borders. Aaand done.
Except for the fontsize and fontname properties in Matlab - I have not figured out why they are not properly exported...but this is not hard to manually fix in Inkscape.
Thanks for your help, everyone.

matlab font weight is set to normal but appears bold

As the title says, it was working fine but now the numbers in the axes appear to be bold, I tried another computer with the same functions and axes appeared normal (set to normal and appeared normal!).
Here's how it looks like:
If I change it to light it would be ok but why I'm getting this?
It has nothing to do with the 'centre axes' function I'm using.
Edit: Not just the axes, titles and labels as well!
Edit2: Everything in MatLab that has a figure like box appears bold! However, when I export a figure it appears normal
Thanks
Perhaps you changed the default fonts. Windows uses Arial by default. Try the following:
set(0, 'defaulttextfontname', 'Arial')
set(0, 'defaultaxesfontname', 'Arial')