Print text on a graph with values, retaining superscripts?

12 visualizaciones (últimos 30 días)
Matt
Matt el 14 de Nov. de 2013
Comentada: Matt el 14 de Nov. de 2013
I have written code (nested for loops) to solve for the constants in the Gompertz Formula. This curve is fit to the data I had gathered. Once the iteration is complete I have values for A, B, and C (the constants in the equation). I want to graph the original data, the fitted curve, and then write the equation on the plot of the fitted curve. The following is the code I used to write the text:
text(Xt,Yt,['Y(t)=',num2str(A),'e^',num2str(B),'e^',num2str(C),'t'])
This works, however when it prints on the graph, the formatting is not ideal. Basically, both B and C are always negative numbers. Immediately following each e, the negative sign is superscript, but everything following it is normal. I found some text tutorials that indicated that placing curly brackets around the text that you want to be superscript should fix this, i.e.
text(Xt,Yt,['Y(t)=',num2str(A),'e^',{num2str(B),'e^',{num2str(C),'t'}}])
but when I run that code I get the error message
Error using text
Cell array of strings may only contain string and numeric matrices
Is there a way to insert the calculated constants in the equation AND retain the equation formatting? Any help would be greatly appreciated... Thanks!

Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Nov. de 2013
text(Xt,Yt,['Y(t)=',num2str(A),'e^{',num2str(B),'}e^{',num2str(C),'}t'])
  1 comentario
Matt
Matt el 14 de Nov. de 2013
Thanks so much! looks like I was putting the curly brackets in the wrong place. I modified your answer a bit:
text(Xt,Yt,['Y(t)=',num2str(A),'e^{',num2str(B),'e^{',num2str(C),'t}}'])
to get the nested exponents.

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 14 de Nov. de 2013
text(Xt,Yt,['Y(t)=',num2str(A),'e^',num2str(B),'e^',num2str(C),'t'])
  2 comentarios
Matt
Matt el 14 de Nov. de 2013
That is the code that I was originally using, and it does print on the graph, however it doesn't retain the superscript formatting that generally follows 'e^' when using the text function. I suspect this is due to the way that the 'e^' function interacts with the num2str function. I am just trying to find out if the string from the num2str function can remain as a superscript after the 'e^'. Thanks!
Azzi Abdelmalek
Azzi Abdelmalek el 14 de Nov. de 2013
Look at this example
A=12;
B=23;
C=15;
plot(1:20);
Bt=num2str(B)
Bt=[repmat('^',1,numel(Bt));Bt]
Ct=num2str(C);
Ct=[repmat('^',1,numel(Ct));Ct]
text(5,5,['Y(t)=',num2str(A),'e',Bt(:)','e',Ct(:)','t'])

Iniciar sesión para comentar.

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by