我正在尝试在MATLAB中使用LaTex字符串作为轴标签,并且没有明显的原因得到一个数字(I'm trying to use a LaTex string for an axis label

编程入门 行业动态 更新时间:2024-10-25 06:21:02
我正在尝试在MATLAB中使用LaTex字符串作为轴标签,并且没有明显的原因得到一个数字(I'm trying to use a LaTex string for an axis label in MATLAB and get a number for no apparent reason)

我正在尝试使用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=A\tau_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()不起作用并产生错误 - 没有产生预期输出其他东西出现的地方。

I'm trying to use a LaTex string to insert a fraction for the y-axis label, and I get a number (in standard font and ylabel position) as well as what I expected (the fraction I'm trying to insert). This has changed for me when I've edited the code, but stopped once I tried investigating this (it's 353.191 as I type, in case it helps). The number is not there if I don't try to add a label to the y-axis, or add a label without LaTex. There is no error message.

Code in question:

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

Full Program (above code is just before program finishes):

% --- 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=A\tau_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('----------');

I'm really confused by this, which makes it harder to find anything. All I could find was basic MatLab help about using LaTex (which is how I muddled the string together) and people having issues where text() wasn't working and generating an error - nothing where the expected output was generated and something else appeared.

最满意答案

TEXT函数返回文本对象的句柄,实际上是一个数字。 这是您作为y-label获得的数字。 您只需要将字符串作为第一个参数传递给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属性不是x和y ,而是轴分数。

TEXT function returns the handle to text object, which is actually a number. This is the number you get as y-label. You only need to pass the string as the first argument to YLABEL and specify the Interpreter (and FontSize) property:

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

Position is determined automatically by ylabel.

In your ylabel statement the text object is actually created (this is why you don't get an error), but the position chosen in such a way that the text is outside of the visible area. -1.25 means the text is located 1.25 of the axes size to the left.

You can also use text object along as axes label, but you will have to adjust the text position with axes size change.

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

Notice, Position property is not x and y, but axes fractions.

更多推荐

本文发布于:2023-08-03 14:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   原因   标签   数字   LaTex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!