Latex, sprintf and variable that changes

Hello everyone,
In the following MATLAB code snippet:
title(sprintf('\\textbf{Product of $\\phi$ with %0.2f delayed $\\phi(t-\\%%0.2f)$ and a = %0.1f}', k(j).*T , a(i)),'Interpreter','latex');
The objective is to create a plot title combining LaTeX formatting with the value of k(j)*T. However, there seems to be an issue with the rendering of the term $\phi(t-\%0.2f)$.
What could be causing the issue in the LaTeX expression, and how can it be fixed to combine LaTeX formatting with the value of k(j)*T effectively?
Thanks in advance
The complete code:
T=1/100;
over=100;
Ts=T/over;
A=4;
a=[0,0.5,1];
k=[0,1,2];
for i=1:length(a)
[phi, t] = srrc_pulse(T,over,A,a(i));
for j=1:length(k)
figure(6*(i-1) + 2*(j-1) + 1);
hold on;
%subplot(2,1,1);
plot(t,phi);
hold on;
title(sprintf('Pulses with a = %0.1f', a(i)));
%subplot(2,1,2);
tdelayed = t+(k(j).*T);
plot(tdelayed,phi);
legend('Original \phi',sprintf('delay %0.2f', k(j).*T));
hold on;
grid on;
% Save the images
filename_subplot = sprintf('subplotWithA=%d_K=%d.png', a(i), k(j));
saveas(gcf, filename_subplot);
phioriginal = [phi zeros(1,k(j)*T/Ts)];
phidelayed = [zeros(1,k(j)*T/Ts) phi];
tmultiplication = [t(1):Ts:t(end)+k(j)*T];
figure(6*(i-1) + 2*(j));
plot(tmultiplication,phioriginal.*phidelayed);
title(sprintf('\\textbf{Product of $\\phi$ with %0.2f delayed $\\phi(t-\\%%0.2f)$ and a = %0.1f}', k(j).*T , a(i)),'Interpreter','latex');
grid on;

 采纳的回答

Your sprintf statement has fields for 3 variables, however you only provide 2.
Provide all 3 and it works —
i = 1;
j = 1;
a(i) = 42;
T = 273;
k(j) = 42^3/1E3;
something_else = rand*100;
title(sprintf('\\textbf{Product of $\\phi$ with %0.2f delayed $\\phi(t-%0.2f)$ and a = %0.1f}', k(j).*T , something_else, a(i)),'Interpreter','latex');
.

4 个评论

The variables being used are:
k(j).*T , a(i)
However, I don't see the third variable. Also, when using your format, the result I am getting is:
Also my using your fomat the result that i am getting is:
You have only supplied two variables in your sprintf statement, however you provided field descriptors for three (in my corrected version of it).
I believe it is supposed to be something like this —
phi = rand;
i = 1;
j = 1;
a(i) = 42;
T = 273;
k(j) = randi(99);
figure
title(sprintf('\\textbf{Product of $\\phi$ with %0.2f delayed $\\phi(t-%0.2f)$ and a = %0.1f}', phi, k(j).*T , a(i)),'Interpreter','latex');
However I don’t know because I don’t know what you want to print in that statement. You need to provide one argument for each edit descriptor.
Note that this:
title(sprintf('\\textbf{Product of $\\phi$ with %0.2f delayed $\\phi(t-\\%%0.2f)$ and a = %0.1f}', k(j).*T , a(i)),'Interpreter','latex')
↑ ← '\\' REPEATED
‘escapes’ (because of the ‘\\’) that particular format descriptor and ‘%%’ prints a single % character so that it prints out as:
figure
title(sprintf('\\textbf{Product of $\\phi$ with %0.2f delayed $\\phi(t-\\%%0.2f)$ and a = %0.1f}', k(j).*T , a(i)),'Interpreter','latex');
which is exactly what you told it to do.
Note — My version prints differently (and likely correctly), and is probably what you want.
.
I realize now that I was fixated on the variable 'something_else' and overlooked the fact that my string actually contains three variables. Thank you for your assistance, and I apologize for the misunderstanding.
As always, my pleasure!
No worries!
No apology necessary. (I probably should have stated initially what I stated in my previous Comment. Still early here.)

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

产品

版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by