How can I write a fraction inside string?

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)');

 Respuesta aceptada

Jan
Jan el 13 de Mayo de 2022
Editada: Jan el 13 de Mayo de 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 comentarios

ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA el 13 de Mayo de 2022
I need to put the dB/dH0 in a fractional way. Not like the simple text
Image Analyst
Image Analyst el 13 de Mayo de 2022
Do you mean like with a horizontal bar between numerator and denominator instead of a slanted bar? I don't know if you can do that but if you can it would probably involve Tex or Latex. Are you familiar with Tex? If not, how important is that to you, really?
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA el 13 de Mayo de 2022
Yes. I tried with latex but was not successful. I am working on that. But so far, no solution.
Jan
Jan el 13 de Mayo de 2022
Editada: Jan el 13 de Mayo de 2022
@ANANTA BIJOY BHADRA: Please do not let me guess, what a "fractional way" is. The forum rules forbid to read the minds of other users due the protection of privacy.
Maybe you mean:
figure, axes
xlabel('Either ^{dB}/_{dH0}')
figure, axes
xlabel('Or:$\frac{dB}{dH0}$', 'Interpreter', 'latex')
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA el 13 de Mayo de 2022
the 2 nd one. But the problem is I can not have it on the string. Here in the xlabel command the xlabel is written like this,
xlabel('Or:$\frac{dB}{dH0}$', 'Interpreter', 'latex')
I know that part. But I need to place it in a srting. Basically, the line 'B-H curve....' works as the figure caption. As I am using loop, I can not change the lines manually. Hence, I need to put it in the code so that it eventually places the line by itslef from the code to the figure> The final figure will be saved as PDF. I hope I am able to express all the informations needed.
But I need to place it in a srting.[sic]
The xlabel function can accept a string array (assuming you're using a sufficiently recent release of MATLAB.)
xlabel("abracadabra")
As I am using loop, I can not change the lines manually. Hence, I need to put it in the code so that it eventually places the line by itslef from the code to the figure
I'm not 100% sure I understand your requirement. Are you saying that you want the numerator and/or denominator of the fraction to be something different in different loop iterations?
figure
varName = 'x';
xlabel("$\frac{" + varName + "}{2}$", 'Interpreter', 'LaTeX');
varName = 'y';
ylabel("$\frac{" + varName + "}{2}$", 'Interpreter', 'LaTeX');
Note that the string I specify in the xlabel and ylabel commands are the same, only the contents of the variable I used in the command are different.
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA el 13 de Mayo de 2022
I put the following part in the code for geeting the xlabel as the figure caption:
clc
clear
y=rand(100,1);
x=1:100;
plot(x,y)
str=sprintf(['B-H curve at f=%d Hz with dB/dH=%.4f with the point where B1=%.2f T\n', ...
'and corresponding H1=%.d (A/m) and \\alpha=%.4f, c=%.2f, k=%.d (A/m)'],1, 2, 3, 4, 5, 6, 7);
xlabel({'H (A/m)', str});
ylabel('B (T)');
Then I find the attached graph. The concern is that I need the place dB/dH as the fraction with the horizontal bar between numerator and denominator instead of a slanted bar. How can I do that?
"But the problem is I can not have it on the string." - It is not clear, what this means. Do you mean the difference between the older CHAR vectors (enclosed in single quotes) and the modern string type (enclosed in double quotes)? Why does this matter? Both are working.
Strings and CHAR vectors consists of characters. There is not way to include LaTeX fraction. Such formatting can appear in lables inside an axes object or in a text-object.
It is no problem to define the string/CHAR vector in a loop:
axes('NextPlot', 'add');
for k = 1:5
s = sprintf('$\\frac{dB}{dH0} = %d$', k);
text(k/10, k/10, s, 'Interpreter', 'latex');
end
You can do the same with xlabels.
So I still do not understand, what you want to achieve.
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA el 13 de Mayo de 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.
@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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Specifying Target for Graphics Output en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 13 de Mayo de 2022

Comentada:

Jan
el 14 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by