How can I added a note on a figure plot in Matlab which contain math symbols (kappa, gamma, alpha, symbol of containing, ...) as explained in the image:
You can add a text label using the text command.
For example
plot(0:10, (0:10).^2);
% Add text at x=5, y=10
text(5, 10, 'this is text');
% Add text at x=5, y=50, with Greek letters
text(5, 50, '\pi is nice, \alpha is too');
This works because the interpreter is set to LaTeX by default. For the full list of supported TeX commands, see this list in the documentation. In particular, since you asked about Greek letters, here is a basic list. Most are as you would expect, preceded by a backslash:
Character Sequence Symbol
\alpha α
\beta β
\gamma γ
\delta δ
\epsilon ɛ
\zeta ζ
\eta η
\theta Θ
\kappa κ
\lambda λ
\mu µ
\xi ξ
\pi π
\rho ρ
\sigma σ
Notes on the LaTeX interpreter.
You can also render maths inside text captions (or axes labels, and chart titles) using $inline mode$ or $$display mode$$. See the linked docs for more details.
You can set the default interpreter to LaTeX (if for some reason it isn't) using
set(0,'defaulttextinterpreter','latex')
The default interpreter is already latex so all you have to do is something like this as an example. not the backslash to access the latex commands. You will have to search what symbols you want on the internet.
legend('\rho = 0.5')
Related
So here's my code:
set(groot, 'defaultAxesTickLabelInterpreter', 'latex') %For axes;
ax = gca;
yticklabels(ax, strrep(yticklabels(ax),'--','–'));
set(ax,'ticklabelinterpreter','tex') %or 'tex' but not 'latex'
figure(1)
t= [0:0.01:2*pi];
x = sin(t);
y = cos(t)
plot(t, x, t, y)
Output:
I tried the solution here, but the hyphens still remain there. I want the en-dash to appear because it's the standard sign for the negative sign. What is the correct way of getting an en-dash to appear instead of the hyphen?
This post at MATLAB Answers explains how to set the (default) interpreter for the axes' labels.
set(groot,'defaultAxesTickLabelInterpreter','latex');
You need to call this before plotting.
Having this set, the tick-labels will be interpreted as LaTeX code. Here is a comparison. The last two examples includes #XiangruiLi's answer (the next code snippets must be called after the plot was created):
yticklabels(gca, strrep(yticklabels(gca),'-','--'));
yticklabels(gca, strrep(yticklabels(gca),'-','$-$'));
none:
latex:
latex + strrep(...,'-','--')):
latex + strrep(...,'-','$-$')):
While the last is probably what you wanted, note that this is certainly not the representation MATLAB intended. It is therefore the question if you really need/want to go through this fuzz.
It seems to me you mis-used strrep. This worked for me:
yticklabels(ax, strrep(yticklabels(ax),'-','--'));
Using the actual Unicode minus character should also work (also in Octave):
yticklabels(gca, strrep(yticklabels(gca),'-','−'));
In this case, there is no need to set the interpreter to LaTeX.
I wanted to print a fraction (\frac{W}{m^2}) and mathematical symbol (Deg c) in the legend of the plot in MATLAB.
Tc = [10,20,30]
legend('300 \frac{W}{m^2}, %.f °C',Tc(1));
It is not working. Giving following error
Error using legend (line 261)
Invalid argument. Type 'help legend' for more information.
Error in DifferentSixePVarrays_configurations_IVcurves (line 95)
legend('300, %.f °C',Tc(1),'400','500','600','700','800','900','1000'); % $$\frac{W}{m^2}$$,%.f °C
It has nothing to do with the maths symbols, you are inputting an integer (Tc(1)) and that is invalid.
It seems that what you want is to create a string using Tc(1). You need to do that in a separate step, not as an input to legend. You can do that with either standard string concatenation, i.e. ['300 ', num2str(Tc(1)), ' °C'] or with sprintf which allows that "C style" string definition (the %f stuff).
On that note, you may need to set the interpreter to Latex, and you may need to add the Latex equivalent to ° for it to work, but this is secondary, not the cause of your error.
I'm wondering how MATLABs latex interpreter for plot text deals with unicode characters? It is weirdly inconsistent. Which, y'know, invalidates the whole ENTIRE point of unicode.
TOY CODE
%*** Setup some text for a plot title
Title_Txt{1} = [char(8734) ,' SNR~~~' , char(10) , '(-)'];
Title_Txt{2} = ['50 SNR~~~' , char(10) , '(-)'];
%*** Plots!
x= 1:1:10
y= rand(size(x))
figure(1)
subplot(211)
plot(x,y)
title(Title_Txt{1} , 'interpreter' , 'latex')
subplot(212)
plot(x,y)
title(Title_Txt{2} , 'interpreter' , 'latex')
Toy code demonstrates that the latex interpreter handles char(10) --- a new line. But it breaks from char(8734) --- the infinity symbol.
Obviously I can work around this by feeding in a latex symbol that matlab knows (another source of frustration but that's for a different discussion), but I am curious about
what MATLAB is doing under the hood here?
is there is a fix for getting unicode into latex?
I suspect the (unsatisfying) answer here is that the Latex interpreter portion of Matlab does whatever the included version of Latex does, and Latex in general doesn't support Unicode. (For Latex solutions, see: https://tex.stackexchange.com/questions/34604/entering-unicode-characters-in-latex. Of course this doesn't help Matlab users.)
As to why Latex doesn't support Unicode. I'll note that the first copyright date on my Latex users' guide is 1985, and the latest release is version 2e, from 1994. Unicode was not really mainstream until the '90's.
(This is a poor answer, but became too long for a comment.)
I would like to use the Letter μ (LaTeX: \mu) in my surface plot. Unfortunately when I use the LaTeX symbol \mu in the zlabel('Power [\muW]'); command it results in:
'Power [ μ W]' instead of 'Power [μW]'
How can I avoid these spaces around μ?
I can't reproduce your problem, which could be due to you using an older version of Matlab. In the past it was neccesary to set the latex interpreter fist, before using Latex syntax. So the following should work:
zlabel('Power [$$\mu$$W]','interpreter','latex');
Nowadays it seems to recognize automatically at least greek letters.
Is it possible using Greek alphabet to represent variables in MATLAB?
For example, I'd like to use the Greek character epsilon as a variable in MATLAB. I tried to insert \epsilon but I received an error.
It is not possible.
I refer to the following part of Matlab documentation:
Valid Names
A valid variable name starts with a letter, followed by letters,
digits, or underscores. MATLAB is case sensitive, so A and a are not
the same variable. The maximum length of a variable name is the value
that the namelengthmax command returns.
Letter is defined as ANSI character between a-z and A-Z.
For example, the following hebrew letter Aleph returns false (in Matlab R2018a returns true):
isletter('א')
By the way, you can always check whether your variable name is fine by using genvarname.
genvarname('א')
ans =
x0x1A
While Andrey's answer is true for variable names it's a different story for figures.
title('\epsilon\omega') will actually work and generate an epsilon and an omega as title (although the matlab font replaces them with different symbols). If you export the figure as an eps or pdf file you will see that the title really is epsilon omega. Actually any LaTeX control sequence will work!
Same is true for all the figure text objects such as legends and axis labels.