Matlab plot title centered

Hi professor,
I am writing to ask if it is possible to adjust the position of titles for all the subplots.

Below are my codes:

load IRF.csv;
load IRF1.csv;

varname=strvcat('Interest Rate (r)','Investment (i)','Tobin''Q (qq) ',...
'Capital (k)','Inflation (\pi)','   Wage (w)','   Consumption (c)',...
'   Output (y)','  Labour (l)','   Capital Utilisation (mpk)',...
'  Premium (pm)','  Real cost of Credit (rk)',...
'  Networth (n)','    M0','    M2');


figure('Units','inches','Position',[1 0.5 13.5 7.5],...
'PaperPositionMode','auto');



for i=1:15
    subplot(3,5,i)
    plot(IRF(:,i),'b','LineWidth',1);
    title(varname(i,:));
    
	hold on;
    plot(IRF1(:,i),'r--','LineWidth',1.5);
	

	set(gca,'Units','normalized',...
	'FontUnits','points',...
	'FontWeight','normal',...
	'FontSize',12,...
	'FontName','Times',...
	'defaultTextInterpreter','latex')
	
	
	ax = gca;
    ax.YAxis.Exponent=0; 

 
	Lgnd = legend('Normal','Crisis');
    set(Lgnd,'FontSize',12,'FontWeight','Bold');
    Lgnd.Position(1) = 0.015;
    Lgnd.Position(2) = 0.2;
	print -depsc2 myplot.eps
end

I want them all center-aligned…

Use

title(deblank(varname(i,:)));

as you character array will fill the array with trailing white spaces.

Unfortunately, it didn’t work…
any convenient ways of creating matlab subplots with all center-aligned titles?
apart from allowing extra spaces insides the single quotation mark?

Best

I would need to see the full codes with all files used.

load IRF.csv
varname=strvcat('Interest Rate (r)','      Investment (i)','     Tobin''Q (pk) ',...
'       Capital (k)','    Inflation (\pi)','       Wage (w)','      Consumption (c)',...
'        Output (y)','        Labour (l)','   Capital Utilisation (mpk)',...
'       Premium (pm)','  Real cost of Credit (rk)',...
'      Networth (n)','  M0','           M2','      Unemployment (u)');


for i=1:15
subplot(4,4,i)
plot(IRF(:,i),'r--','LineWidth',1.5)
axis tight;
set(gca,'FontSize',12, 'FontName','Times') 
ax = gca;
ax.YAxis.Exponent=0; 
title(deblank(varname(i,:)));
end

It should be

varname=char('Interest Rate (r)','Investment (i)','Tobin''Q (pk)',...
'Capital (k)','Inflation (\pi)','Wage (w)','Consumption (c)',...
'Output (y)','Labour (l)','Capital Utilisation (mpk)',...
'Premium (pm)','Real cost of Credit (rk)',...
'Networth (n)','M0','M2','Unemployment (u)');


for i=1:15
subplot(4,4,i)
plot(randn(10,1),'r--','LineWidth',1.5)
axis tight;
set(gca,'FontSize',12, 'FontName','Times') 
ax = gca;
ax.YAxis.Exponent=0; 
title(deblank(varname(i,:)));
end
1 Like

your solution worked perfectly! thanks!
Another question regarding matlab plotting function is:
Is it possible to insert an overall title on the center top above all these subplots?
I know there is a function called “sgtitle” or “suptitle”, but neither worked with my version R2018a

best

In principle, you can use Matlab’s text function, but that is cumbersome.