Error using latex in colorbar title

9 visualizaciones (últimos 30 días)
Nathaniel H Werner
Nathaniel H Werner el 19 de Dic. de 2019
Comentada: Subhadeep Koley el 7 de En. de 2020
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 comentario
Walter Roberson
Walter Roberson el 19 de Dic. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Subhadeep Koley
Subhadeep Koley el 7 de En. de 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 comentarios
Nathaniel H Werner
Nathaniel H Werner el 7 de En. de 2020
Should I accept this as a workaround until a solution is arrived at?
Subhadeep Koley
Subhadeep Koley el 7 de En. de 2020
Yes, you can accept this workaround.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Formatting and Annotation en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by