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)');
采纳的回答
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 个评论
I need to put the dB/dH0 in a fractional way. Not like the simple text
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?
Yes. I tried with latex but was not successful. I am working on that. But so far, no solution.
@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')

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.
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.
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.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
