问题
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