Error using latex in colorbar title

17 views (last 30 days)
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 Comment
Walter Roberson
Walter Roberson on 19 Dec 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.

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!