我正在创造一个这样的传奇,
theta = [90 120 80 120];phi = [120 120 180 180];对于i = 1:长度(theta) plot_legend {i} = sprintf(‘\ theta =%3d \ circ \ phi =%3d \ circ’,theta(i),phi(i))结束这个 …
以下是使用LaTeX解释空格替换第一个填充零的一般解决方案:
% --- Definition theta = [5 120 80 120]; phi = [120 120 180 180]; % --- The function handle that does the job f = @(x, n) regexprep(num2str(x, ['%0' num2str(n) 'i']), '^(0+)', '${repmat(''\\hspace{0.5em}'', [1 numel($0)])}'); % --- Create a plot and the legend hold on for i = 1:length(theta) % Fake curve ezplot(['x^' num2str(i)]); plot_legend{i} = ['$\theta=' f(theta(i),3) '^\circ,\ \phi=' f(phi(i),3) '^\circ$']; end legend(plot_legend, 'interpreter', 'latex')
结果如下:
您可以使用函数句柄的第二个参数指定数字的长度。请注意,这仅适用于整数值。
最好,