Error using latex in colorbar title

17 visualizzazioni (ultimi 30 giorni)
Hello,
I'm trying to make my color bar have a title, and I want to give it a math equation. Here is my code.
cbh = findall(fh, 'Type', 'ColorBar');
cTH = get(cbh,'Title');
set(cTH,'String',['$',var,'(r)/','\underset{\text{LEV}}{\text{mean}}(',var,')$'])
The variable var is just a string in my code, you can make it any letter.
After getting to the third line in my code I get this error, and $var(r)/\underset{\text{LEV}}{\text{mean}}(var)$ showed up as the title for my colorbar.
Warning: Error updating ColorBar.
String scalar or character vector must have valid interpreter syntax:
However, if I open a live script in Matlab and type the content of the LaTeX script and run it successfully gives the equation.
I thought maybe the interpreter was something other than LaTex, so I tried changing it with the methods below but nothing happened. But I did go and check and they were already set as latex.
cbh.Title.Interpreter = 'latex';
cTH.Interpreter = 'latex';
So why am I getting this error? I checked in other LaTeX packages, and the syntax is correct. I also looked into the MatLab documentation and it seems like I am doing everything correct.
  1 Commento
Walter Roberson
Walter Roberson il 19 Dic 2019
I recommend using sprintf to construct the title character vector into a variable and then setting the property from the variable. This makes for more robust debugging as you can check for possibilities such as odd characters such as could occur if var was numeric.
If you do use sprintf remember that \ in the format needs to be doubled to make it into the output.

Accedi per commentare.

Risposta accettata

Subhadeep Koley
Subhadeep Koley il 7 Gen 2020
Hi, I can reproduce the same issue in my end also. I have written a workaround. Try if that works. Meanwhile we are looking into this.
fh=figure; surf(peaks); shading flat; axis tight;
colorbar;
cbh = findall(fh, 'Type', 'ColorBar');
cTH = get(cbh,'Title');
set(cTH,'String',['$','u_t','(r)/','\mathop{}_{\scriptstyle{LEV}}^{\rm{mean}}(','u_t',')$'],'Interpreter','latex','fontsize',17);
LaTeX.png
Hope this helps!
  2 Commenti
Nathaniel H Werner
Nathaniel H Werner il 7 Gen 2020
Should I accept this as a workaround until a solution is arrived at?
Subhadeep Koley
Subhadeep Koley il 7 Gen 2020
Yes, you can accept this workaround.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by