how to write numbers with 10^ format in matlab?

How to print 123456 as 1.2x10^5 in a matlab figure?
str5 = ['CC = ' num2str(123456) ];
str = sprintf('%s',str5);
annotation('textbox',[0.1 0.1 0.1 0.1],'String',str,'FitBoxToText','on');
Any help will be greatly appriciated.

 Accepted Answer

str = strrep(sprintf('CC = %.1e',123456),'e','x10^')
str = 'CC = 1.2x10^+05'
str = regexprep(sprintf('CC = %.1e',123456),'e(\D)0*(\d+)$','x10^$1$2')
str = 'CC = 1.2x10^+5'

4 Comments

Thank you so much for your quick reply.
There seems some error. It worked only partially.
Why does that appear like this, when I use
str = strrep(sprintf('CC = %.1e',58632),'e','x10^')
MP
MP on 10 Aug 2022
Edited: MP on 10 Aug 2022
@Stephen23: It didnt work properly.
Please see my comment.
"Why does that appear like this"
This is due to how the graphics engine interprets text. You need to either:
str = strrep(sprintf('CC = %.1e',123456),'e','x10^')
str = 'CC = 1.2x10^+05'
plot(0:1,0:1)
annotation('textbox',[0.5,0.5,0.1,0.1],'String',str,'interpreter','none')
str = strrep(sprintf('CC = %.1e',123456),'e','x10\^');
worked!!
Thank you so much @Stephen23

Sign in to comment.

More Answers (0)

Categories

Asked:

MP
on 10 Aug 2022

Commented:

MP
on 11 Aug 2022

Community Treasure Hunt

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

Start Hunting!