How can I write a fraction inside string?

131 Ansichten (letzte 30 Tage)
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA am 13 Mai 2022
Kommentiert: Jan am 14 Mai 2022
I have a plot where I put the graph's parameters in a string and put xlabel as the unit on X-axis. Inside the string, I need to have the below line "B-H curve at f=%d Hz with dB/dH0=%.4f with the point (B1, H1) and alpha=%.4f, c=%.2f, k=%.d" How can I write that? I have given a sample code below:
clc
clear
y=rand(100,1);
x=1:100;
plot(x,y)
str=sprintf(??????????????????????????????????????????????????????????????????????);
xlabel({'H (A/m)',str});
ylabel('B (T)');

Akzeptierte Antwort

Jan
Jan am 13 Mai 2022
Bearbeitet: Jan am 13 Mai 2022
What exactly is the problem? I've simply copied the string provided in the question, inserted a linebreak \n to let the comnplete text be inside the limits and inserted some dummy data:
y=rand(100,1);
x=1:100;
plot(x,y)
str=sprintf(['B-H curve at f=%d Hz with dB/dH0=%.4f with the point\n', ...
'(B1, H1) and alpha=%.4f, c=%.2f, k=%.d)'], 1, 2, 3, 4, 5);
xlabel({'H (A/m)', str});
ylabel('B (T)');
Looks trivial. Do I oversee anything?
  10 Kommentare
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA am 13 Mai 2022
I need to make a figure caption. That is my main target. I have attached an image along with the comment. Hope it helps.
Jan
Jan am 14 Mai 2022
@ANANTA BIJOY BHADRA: We have showed you, how to display the wanted fraction as xlabel. It is trivial to insert the other part of the text by your own. So what is still the problem?
xlabel(['Put what you want here $\frac{dB}{dH0}$', ...
char(10), ' and here'], 'Interpreter', 'latex')
You got examples already for inserting values in a string
str=sprintf(['B-H curve at f=%d Hz with dB/dH0=%.4f with the point\n', ...
'(B1, H1) and alpha=%.4f, c=%.2f, k=%.d)'], 1, 2, 3, 4, 5);
All you have to do is to combine these two methods.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by