How can I write the value of the array as an exponent in sprintf

9 visualizzazioni (ultimi 30 giorni)
Hello, I want write the following function in the legend of my graph: f(x) = a * x^n.
I calculated a and n. Now I want to replace a and n with the corresponding value and write it as a string. So I want to use the command sprintf:
sprintf('\\rm{f(x) = %0.4g * x^%0.3g}', a, n)
The only problem is that I can't get the value for n as an exponent. How can you do that? Is it possible with sprintf?
Thank you for any help!

Risposta accettata

Matt Fig
Matt Fig il 29 Giu 2011
Works here:
plot(1:10)
legend(sprintf('f(x) = %0.4g * x^%0.3g', .3, 3))
EDIT In response to comments.
In that case:
legend(sprintf('f(x) = %0.4g * x^{%0.3g}', -.3, -3))
  3 Commenti

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by