MATLAB tip of the day: Toggling scales
/The Problem
Some of the figures I create can be presented on either logarithmic or linear scales. After creating a plot, I may want to switch scales, but I don't want to type set(gca, 'XScale', 'log')
every time (nor do I want to pull it out of the Command History). I want a faster way to switch between lin
and log
axes.
My Solution
A shortcut, of course. I love shortcuts. This shortcut determines the current scale used for the selected axis, and switches the scaling to the other choice.
Here's the code:
% Toggle scale of Y axis
scaleType = get(gca, 'YScale');
switch scaleType
case 'linear'
set(gca, 'YScale', 'log')
disp('y scale is now log')
case 'log'
set(gca, 'YScale', 'lin')
disp('y scale is now linear')
end
I have two such shortcuts, one for X, and one for Y. (Obviously, for X, you swap in 'XScale'
for 'YScale'
.)