How to change display format of legend in MATLAB

瘦欲@ 提交于 2019-12-10 17:54:33

问题


I am looking for a way to force the legend entries in a particular format. I following code, they are displayed like

Instead I want it like 1e-1,1e-2,1e-3,1e-4, 1e-5. Is there a way to do this.

MWE:

sig=[0.1 0.01 0.001 0.0001 0.00001];
for j=1:length(sig)
    for x=1:10
       Cost(j,x) = 2*x+j;
    end 
plot(1:10,Cost(j,:));
end 
legend(strcat('\sigma^2_n=',num2str((sig)')));
set(h,'Interpreter','latex')

回答1:


You should specify that you'd like to use scientific notation when you convert sig to a string by passing a custom format specifier to num2str

legend(strcat('\sigma^2_n=',num2str(sig.', '%.0e')));

If you want to remove the leading 0 in the exponent, you can remove them with a regular expression

S = regexprep(cellstr(num2str(sig.', '%.0e')), '(?<=e[-+])0*', '');
legend(strcat('\sigma^2_n=', S))



来源:https://stackoverflow.com/questions/43074324/how-to-change-display-format-of-legend-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!