How do I put variable values into a text string for Legend

I want to put the variable as a legend but in form of power of some amount this is the variable
x=-1.1;
then in the result i want to see 10^-1.1
figure
plot(Ratio,Final_1,'k-x','linewidth',2)
grid on
legendInfo = ['SNR target for BER 10^' num2str(x)];
legend(legendInfo,'Location','southeast')
xlabel('Ratio')
ylabel('SNR')
problem i have is this part "'SNR target for BER 10^' num2str(x)" because it is not showing like 10^x

 採用された回答

Star Strider
Star Strider 2017 年 2 月 5 日

1 投票

Use the sprintf function:
legendInfo = sprintf('SNR target for BER 10^%f', x);

5 件のコメント

Hamid Reza Barzegar
Hamid Reza Barzegar 2017 年 2 月 5 日
result is like this
but i need like this one
Star Strider
Star Strider 2017 年 2 月 5 日
This works:
legendInfo = sprintf('SNR target for BER 10^{%2d}', floor(x));
I tested it.
The floor function rounds to -Inf, and allowing a two-digit field with ‘%2d’ makes room for the sign.
Hamid Reza Barzegar
Hamid Reza Barzegar 2017 年 2 月 5 日
Thanks, It's correct. but do you know how to write for float number. Imagine x=-1.2
I need to present it like this
because when i floor(x) it shows another value
Hamid Reza Barzegar
Hamid Reza Barzegar 2017 年 2 月 5 日
I found it.
legendInfo = sprintf('SNR target for BER 10^{%0.1f}', (x));
Thanks for your reply.
Star Strider
Star Strider 2017 年 2 月 5 日
My pleasure.
I thought you wanted integers rounded to the next lowest integer. The '%.1f' is the correct option for one decimal-place floating-point precision.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by