Renart
Renart

Reputation: 123

Variable as an exponent in a string (Octave)

For an exercise i have to create an animated plot of the functions x^i from i=1 to i=10 in octave. The plot has to have a dynamic title and the color of the curves must change. Currently my code is the following :

function exercicequatorze
  x=linspace(0,1,100);
for i=1:10
  plot(x,x.^i,'color',[i/10,0,1-i/10])
  a=num2str(i);
  title(["Le graphe de la fonction x^",a] )
  pause(1)
endfor
endfunction

and it performs pretty much as intended, I was even surprised to get x¹, x², x³, ... in my title instead of x^1, x^2 and so on. However, at the last plot my title ends in x¹0 instead of x¹⁰. Any idea on how to fix that problem ? I tried fiddling with braces but I either get a syntax error or the number that should be my exponent get shifted to a second line in the title.

Upvotes: 2

Views: 275

Answers (1)

rahnema1
rahnema1

Reputation: 15837

You need to place the number in curly braces.

title(["Le graphe de la fonction x^{" a "}"])

It is explained in the documentation:

If the '^' or '_' is followed by a { character, then all of the block surrounded by the { } pair is superscripted or subscripted. Without the { } pair, only the character immediately following the '^' or '_' is changed.

Upvotes: 2

Related Questions