我正在尝试在 MATLAB 中使用 LaTex 字符串作为轴标签,并无缘无故地获得一个数字



我试图使用LaTex字符串为y轴标签插入一个分数,我得到了一个数字(标准字体和ylabel位置)以及我想要的(我试图插入的分数)。当我编辑代码时,这对我来说已经改变了,但当我尝试调查它时就停止了(我键入的是353.191,以防有帮助)。如果我不尝试在y轴上添加标签,或者添加没有LaTex的标签,那么数字就不存在了。没有错误消息。

有问题的代码:

ylabel(text('Interpreter','LaTex',...
    'string','$frac{tau_b(t)}{phi bar{U}}$',...
    'FontSize',20,'position',[-1.25,0.2]));

完整程序(以上代码仅在程序结束前):

% --- MM3CAI Coursework 1 ---
clear all; clf('reset'); clc;
fig_num=0;
disp('Started program');
disp(' ');
% --- Task 1. About the water-brake only ---
disp('Started task 1');
disp(' ');
w_t=0.003;          % Volume of water in the brake at time t        [m^3]
thet_t=250;         % Angular velocity of brake at time t           [rads^-1]
percent=0.1;        % Percent added to values for small change      [%]
fraction=percent/100;
del_w=w_t*fraction;
del_t=thet_t*fraction;
w_del=w_t+del_w;
thet_del=thet_t+del_t;
clear percent fraction;
% --- Q1 ---
disp('Started question 1');
disp(' ');
tau  =150*w_t  *thet_t;
tau_w=150*w_del*thet_t;
tau_t=150*w_t  *thet_del;
tau_mat=[tau;...
     tau_w;...
     tau_t];
A=[w_t   thet_t   1;...
w_del thet_t   1;...
w_t   thet_del 1];
variables_mat=Atau_mat;
phi=variables_mat(1,1);
psi=variables_mat(2,1);
eta=variables_mat(3,1);
disp(['Phi = ', num2str(phi)]);
disp(['Psi = ', num2str(psi)]);
disp(['Eta = ', num2str(eta)]);
disp(' ');
disp('Finished question 1');
disp('----------');
% --- Q2 ---
disp('Started question 2');
disp(' ');
beta=-eta/phi;
disp(['Beta = ', num2str(beta)]);
disp(' ');
disp('Finished question 2');
disp('----------');
% --- Q4 ---
disp('Started question 4');
disp(' ');
G=@(omega) phi./(1+(5i.*omega));
frequency=logspace(-3,3,700)';
G_mat=G(frequency);
phase_mat_rad=angle(G_mat);
phase_mat_deg=phase_mat_rad.*(180/pi);
magnitude_mat=abs(G_mat);
gain_mat=20.*log10(magnitude_mat);
fig_num=fig_num+1;
figure(fig_num);
subplot(2,1,1);
semilogx(frequency,gain_mat);
title('Bode Plot');
xlabel('Frequency [rads^-^1]');
ylabel('Gain [dBs]');
subplot(2,1,2);
semilogx(frequency,phase_mat_deg);
xlabel('Frequency [rads^-^1]');
ylabel('Phase Angle [degrees]');
disp('Finished question 4');
disp('----------');
% --- Q5 ---
disp('Started question 5');
disp(' ');
U_bar=1;
step=@(t) (phi*U_bar)*(1-exp(-t/5));
time=(0:0.01:8);
step_mat=step(time);
normalised=step_mat./(phi*U_bar);
fig_num=fig_num+1;
figure(fig_num);
plot(time,normalised);
title('Step Response');
xlabel('Time [s]');
ylabel(text('Interpreter','LaTex',...
        'string','$frac{tau_b(t)}{phi bar{U}}$',...
        'FontSize',20,'position',[-1.25,0.2]));
disp('Finished question 5');
disp('----------');

我真的很困惑,这让我很难找到任何东西。我所能找到的只是MatLab关于使用LaTex的基本帮助(这就是我将字符串混淆在一起的方式),以及text()不起作用并生成错误的问题——在生成预期输出时没有任何帮助出现了其他内容。

TEXT函数返回文本对象的句柄,该对象实际上是一个数字。这是你作为y标签得到的数字。您只需要将字符串作为第一个参数传递给YLABEL,并指定Interpreter(和FontSize)属性:

ylabel('$frac{tau_b(t)}{phi bar{U}}$','Interpreter','LaTex','FontSize',20);

位置由ylabel自动确定。

ylabel语句中,实际上创建了文本对象(这就是为什么不会出现错误的原因),但选择的位置使文本位于可见区域之外-1.25表示文本位于轴尺寸的1.25的左侧。

您也可以使用文本对象作为轴标签,但您必须随着轴大小的变化调整文本位置。

text('Interpreter','LaTex',...
    'string','$frac{tau_b(t)}{phi bar{U}}$',...
    'FontSize',20,'position',[-0.1,0.5]);

请注意,Position属性不是xy,而是轴分数。

最新更新