Error using latex in colorbar title

12 ビュー (過去 30 日間)
Nathaniel H Werner
Nathaniel H Werner 2019 年 12 月 19 日
コメント済み: Subhadeep Koley 2020 年 1 月 7 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 12 月 19 日
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.

サインインしてコメントする。

採用された回答

Subhadeep Koley
Subhadeep Koley 2020 年 1 月 7 日
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 件のコメント
Nathaniel H Werner
Nathaniel H Werner 2020 年 1 月 7 日
Should I accept this as a workaround until a solution is arrived at?
Subhadeep Koley
Subhadeep Koley 2020 年 1 月 7 日
Yes, you can accept this workaround.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColorbar についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by