Multi-line Titles in LaTeX on Response Plots

28 visualizzazioni (ultimi 30 giorni)
So by combining several tips and tricks from past articles on this website, I have come up with the some code in an attempt to make multi-line titles of multiple step responses (subplots) in one figure with a variable that changes. All of this inside a for loop. Then I got stuck.
Basically what it boils down to: A regular plot can accept multiple-line titles but these response plots cannot. The following code will do what I need it to do:
plot(1:10)
title({'First line','Second line'})
However, this will not (where sys is a transfer function)
step(sys)
title({'First line','Second line'})
This throws errors:
??? Parameter must be a string.
Error in ==> ctrluis.axesgroup.addbypass>localTitle at 25
this.Title = string;
Error in ==> mwbypass at 18
hh = feval(fcn{:},varargin{:});
Error in ==> title at 38
h = mwbypass(ax,'MWBYPASS_title',string,pvpairs{:});
Error in ==> title at 23
h = title(gca,varargin{:});
For those who want to pursue what I am doing, here's all of my code:
format long
syms s;
t = 1:0.01:10;
omega_n = 2.47;
zeta = [0.4, 0.7, 1.0, 2.0];
for i = 1:length(zeta)
N = omega_n^2;
D = s^2 + 2*zeta(i)*omega_n*s + omega_n^2;
sys = simplify(expand(N/D));
[N, D] = numden(sys);
num = sym2poly(N);
den = sym2poly(D);
sys = tf(num, den);
subplot(2,2,i)
step(sys, t);
subtitle = cellstr(char(['\makebox[4in][c]{' 'Step Response of ' '$$G(s)$$}'],...
['\makebox[4in][c]{' '$$\omega_n=2.47$$' ' and ' '$$\zeta=' num2str(zeta(i)) '$$}']));
title(subtitle,...
'FontSize', 12,...
'interpreter', 'latex',...
'FontName', 'Times')
end
  2 Commenti
Craig
Craig il 18 Nov 2011
What version of MATLAB are you using?
Matt
Matt il 22 Nov 2011
This is 2009a 7.8.0.

Accedi per commentare.

Risposta accettata

Matt
Matt il 22 Nov 2011
Hey all, I found the answer. Newline character for use within parbox is '\\'
So the final code would look something like this:
subtitle = ['\parbox[b]{2in}{\centering Step Response of ' '$$G(s)$$\\ $$\omega_n=2.47$$' ' and ' '$$\zeta=' num2str(zeta(i)) '$$}'];
Just be careful with how many lines you put up in a subplot title. LaTeX isn't given much space for titles in Matlab (if any at all), so some of my text is cut off. You might need to adjust the FontSize of the text to get it all to display properly.
Thank you for all who helped.

Più risposte (4)

Matt Fig
Matt Fig il 16 Mar 2011
So trick MATLAB!
close all % So you know I am starting fresh
T = title({'First line','Second line'});
set(gca,'visible','off')
set(T,'visible','on')
step(sys) % Make your other plot here.
You can do most anything here, including just calling TEXT to make your title. A title is just a text object.
  1 Commento
Matt Fig
Matt Fig il 16 Mar 2011
Perhaps you need to create another axes object for the STEP function to use. Do this after setting the title to visible. Or possibly set the handlevisibility of the invisible axes to off at the same time it is made invisible.

Accedi per commentare.


Matt
Matt il 16 Mar 2011
Matt,
Thanks for the quick response. I tried your code but still got an error:
??? Error using ==> lti.step at 86
Parameter must be a string.
  1 Commento
Matt Fig
Matt Fig il 16 Mar 2011
Perhaps you need to create another axes object for the STEP function to use. Do this after setting the title to visible. Or possibly set the handlevisibility of the invisible axes to off at the same time it is made invisible.

Accedi per commentare.


Matt
Matt il 16 Mar 2011
Here's what I ended up doing. Combination of parbox or vbox (you pick). Parbox seemed to work the best and got the title more towards the center of each plot. The actually text alignment is a little funky, and I don't know LaTeX well enough to figure out why it is the way it is. I was trying to get the lines centered, but it appears the bottom line is centered and the top ignores justification.
subtitle = ['\parbox[b]{2in}{\centering Step Response of ' '$$G(s)$$\newline',...
'$$\omega_n=2.47$$' ' and ' '$$\zeta=' num2str(zeta(i)) '$$}'];
title(subtitle,...
'FontSize', 12,...
'interpreter', 'latex',...
'FontName', 'Times')

Jeffrey Daniels
Jeffrey Daniels il 16 Nov 2011
Have you tried this?
subtitle = ['\parbox[b]{2in}{\centering Step Response of ' '$$G(s),10,... '$$\omega_n=2.47$$' ' and ' '$$\zeta=' num2str(zeta(i)) '$$}'];
  2 Commenti
Matt
Matt il 22 Nov 2011
Jeffrey,
Thanks for your reply. I no longer have the original project I was working on, but I am using the parbox in another piece of code in the same manner.
I'm not sure what the "10" does, however when I remove "\newline" from the string array the text is perfectly centered (but isn't forced onto a second line until it hits the 2in width of the parbox).
So now I need to figure out a better way to force text onto a new line within parbox. Do you know what I could do?
Walter Roberson
Walter Roberson il 22 Nov 2011
The 10 should be char(10) which is the newline character.

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by