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 件のコメント
ANANTA BIJOY BHADRA
2022 年 5 月 13 日
I need to put the dB/dH0 in a fractional way. Not like the simple text
Image Analyst
2022 年 5 月 13 日
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
2022 年 5 月 13 日
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')

ANANTA BIJOY BHADRA
2022 年 5 月 13 日
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
2022 年 5 月 13 日
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?
Jan
2022 年 5 月 13 日
"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
2022 年 5 月 13 日
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
2022 年 5 月 14 日
@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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
