# Putting alpha in a string

1 view (last 30 days)
ANANTA BIJOY BHADRA on 21 Apr 2022
Commented: Voss on 21 Apr 2022
I have to make a figure caption from the m.file. The caption contains greek letters alpha. I was trying put it in thwe caption as the part of xlabel using the string command. But it does not work. I can not edit from the figure properties as I have to implement it in a loop. Also I need to put 10^6 inside the caption. How can I solve these problems? The sample code is below,
clc
clear
x=rand(10,1)
f=60
Ms_peak=1.8;
alpha=0.0001;
c=0.26;
a=1500;
k=400;
plot(x)
str=sprintf('B-H Curve at Frequency=%d for M{_s}=%.2f, \alpha=%.4f, c=%.2f, a=%d, k=%.d',f,Ms_peak,alpha,c,a,k); %Figure caption.
xlabel({'H (A/m)',str});
ylabel('B (T)');

Matt J on 21 Apr 2022
Edited: Matt J on 21 Apr 2022
Note the double slash in '\\alpha' as well as Interpreter='tex'.
str=sprintf('B-H Curve at Frequency=%d for M{_s}=%.2f, \\alpha=%.4f, c=%.2f, a=%d, k=%.d',f,Ms_peak,alpha,c,a,k);
xlabel({'H (A/m)',str},Interpreter='tex'); ANANTA BIJOY BHADRA on 21 Apr 2022
Thank you very much for the help.

Voss on 21 Apr 2022
Use double-backslash to escape the backslash in sprintf, so that what's returned from sprintf contains '\alpha'.
Not clear where 10^6 should go, but here it is.
clc
clear
x=rand(10,1);
f=60;
Ms_peak=1.8;
alpha=0.0001;
c=0.26;
a=1500;
k=400;
plot(x)
str=sprintf('B-H Curve at Frequency=%d for M{_s}=%.2f, \\alpha=%.4f, c=%.2f, a=%d, k=%.d',f,Ms_peak,alpha,c,a,k); %Figure caption.
xlabel({'H (A/m) [ x 10^6 ]',str});
ylabel('B (T)'); ##### 2 CommentsShowHide 1 older comment
Voss on 21 Apr 2022
Modified, as instructed:
clc
clear
x=rand(10,1);
f=60;
Ms_peak=1.8;
alpha=0.0001;
c=0.26;
a=1500;
k=400;
plot(x)
str=sprintf('B-H Curve at Frequency=%d for M{_s}10^6=%.2f, \\alpha=%.4f, c=%.2f, a=%d, k=%.d',f,Ms_peak,alpha,c,a,k); %Figure caption.
xlabel({'H (A/m)',str}); % 'tex' is default Interpreter, not necessary to specify it
ylabel('B (T)'); R2021a

### Community Treasure Hunt

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

Start Hunting!