Running into problems using the latex interpreter in strings

I have attached a code snippet that I'm trying to execute (Matlab Ver. 2024b):
I get warnings as follows:
Warning: Error in state of SceneNode.
String scalar or character vector must have valid interpreter syntax:
Inband Radiance, $\\left[\\frac{W}{\\mathrm{m}^2\\,\\mathrm{sr}}\\right]$
> In defaulterrorcallback (line 12)
In matlab.graphics.illustration/ColorBar/get.Label
Warning: Error in state of SceneNode.
String scalar or character vector must have valid interpreter syntax:
Event # 0089, Filter Wheel #1: Spectral Range = 1.97-5.39 $\\mu$m
The colorbar label looks ok to me, but the interpreter in the figure title is not interpreted at all,and just shows up as $\mu$ in the title (see the attached. .png file). Could someone please let me know what I am doing incorrectly?

 Accepted Answer

There are two issues, mainly.
(1) You should set the Interpreter first (before the String), to avoid the warnings
(2) Because it's LaTeX, your pound signs need slashes: \#
XCoors = 1:320;
YCoors = 1:256;
A = rand(numel(XCoors),numel(YCoors));
spRangeSel = ' 1.97-5.39';
channelNum = 1;
eventNum = 1;
fig = figure;
figWidth = 0.65;
figHeight = figWidth*XCoors(end)/YCoors(end);
set(gcf,'units','normalized','Position',[(1-figWidth)/2 (1-figHeight)/2 figWidth figHeight],'color','white');
h=imagesc(XCoors,YCoors,A); shading interp; colormap hot; axis equal
a = colorbar;
a.Label.Interpreter = 'latex';
a.Label.String = 'Inband Radiance, $\left[\frac{W}{\mathrm{m}^2\,\mathrm{sr}}\right]$';
ax1 = gca;
ax1.FontSize=20;
xlabel(ax1,'X [mm]','Fontsize',24,'interpreter','latex');
ylabel(ax1,'Y [mm]','Fontsize',24,'interpreter','latex');
ax1.Title.Interpreter = 'latex';
ax1.Title.String = ['Event \# ',num2str(eventNum,'%4.4d'),', Filter Wheel \#',int2str(channelNum),': Spectral Range =',spRangeSel,' $\mu$m'];
ax1.Title.FontName = 'Times';

1 Comment

Thank you! I forgot I needed to escape the '#' symbols too!

Sign in to comment.

More Answers (0)

Products

Release

R2024b

Asked:

MBP
on 28 Jul 2025

Commented:

MBP
on 29 Jul 2025

Community Treasure Hunt

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

Start Hunting!