Putting alpha in a string

15 views (last 30 days)
ANANTA BIJOY BHADRA
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)');

Accepted Answer

Matt J
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');
  3 Comments
Matt J
Matt J on 21 Apr 2022
clc
clear
x=rand(10,1)
x = 10×1
0.0249 0.7662 0.7388 0.4406 0.4688 0.7279 0.9047 0.3438 0.9676 0.7423
f=60
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\\times10^6, \\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)');
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA on 21 Apr 2022
Thank you very much for the help.

Sign in to comment.

More Answers (1)

Voss
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 Comments
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA on 21 Apr 2022
Thank you. the 10^6 has to go after the M_s.
Voss
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)');

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!