Writing an exponential equation in the title

28 views (last 30 days)
Akhil Vasvani
Akhil Vasvani on 6 Mar 2017
Moved: Sam Chak on 2 Oct 2024
I would like to write my exponential equation in the title of my figure. Right now I have:
title(['y = ',num2str(a),' x^', (num2str(n))]);
The problem is my equation comes out as: y = 8.56 x^(0).68, which is not what I want.
I want would the title to be y = 8.56 x^(0.68). How do I do this?
  1 Comment
Abdoulaye
Abdoulaye on 2 Oct 2024
Moved: Sam Chak on 2 Oct 2024
str=num2str(n);
temp=[1:length(str) 0.5:(length(str)-0.5)];
str=[str repmat('^',1,length(str))];
[~,temp]=sort(temp);
str=str(temp);
if true title(['y = ',num2str(a),' x', str]); end

Sign in to comment.

Answers (3)

John BG
John BG on 7 Mar 2017
Edited: John BG on 7 Mar 2017
Akhil
you mean this
a=8.56;n=0.68; figure; title(['y = ' num2str(a) ' x^{' num2str(n) '}' ]);
.
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

Walter Roberson
Walter Roberson on 6 Mar 2017
title( sprintf('y = %.2f x^{%.2f}', a, n) );

Rik
Rik on 6 Mar 2017
This syntax is essentially calling TeX, so it follows that syntax. It doesn't, however support accolades for grouping commands, so you'll have to repeat the ^ sign.
I suspect there is faster, more stable and more elegant way to do it, but this should work:
str=num2str(n);
temp=[1:length(str) 0.5:(length(str)-0.5)];
str=[str repmat('^',1,length(str))];
[~,temp]=sort(temp);
str=str(temp);
if true title(['y = ',num2str(a),' x', str]); end
  2 Comments
Walter Roberson
Walter Roberson on 7 Mar 2017
I do not think I have ever seen "accolades" used in that context??
TeX uses {} to group; the approach I posted in my Answer works fine.
Rik
Rik on 8 Mar 2017
'Accolade' is the Dutch word for the curly bracket, so that's why I used that word (thinking the English word would also cover this meaning).
Some time ago I have tried to get subscript and superscript in my xlabel and ylabel, but there the {} didn't seem to work, so I assumed that it would work in the title either.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!