Matlab changing y-axis tick values

410 Views Asked by At

I have a plot in which the y-axis ticks are like 2.400400e-01. I do not like this, I need to make it shorter and understandable. How is it possible? I used :

set(gca, 'YTickLabel', num2str(get(gca,'YTick')','%d')) 
1

There are 1 best solutions below

0
On

I have found an answer from MathWorks. I will share it for future reference. This gives what we are looking for.

`hAx = gca;                                                      % Get Axis Handle
% hAx.YScale = 'log';                                           % Set Y-Scale To Logarithmic
ytix = hAx.YTick;                                               % Get Y-Tick Values
ytix_exp = fix(log10(ytix));                                    % Y-Tick Label Exponents
ytix_exp(~isfinite(ytix_exp)) = 0;                              % Set ‘-Inf’ To ‘0’
ytix_mnt = 10.^rem(log10(ytix),1);                              % Y-Tick Label Mantissas
ytix_mnt(~isfinite(ytix_mnt)) = 0;                              % Set ‘NaN’ To ‘0’
% ytkstr = sprintf('%.6f 10^{%d}\n', [ytix_mnt; ytix_exp]);       % Create Strings For Y-Labels, Choose Mantissa Format As: '%.5f'
ytkstr = sprintf('%.6f 10^{%d}\n', [ytix_mnt; ytix_exp ]);
ytix_lbl = regexp(ytkstr, '\n', 'split');                       % Separate Strings To Create Y-Tick Labels
ytix_lbl{1} = '0';                                              % Set First Y-Yick Label To '0'
set(hAx, 'YTick',ytix', 'YTickLabel',ytix_lbl) `