How to show fractional values in plot

7 vues (au cours des 30 derniers jours)
Mike
Mike le 6 Oct 2012
How do you show fractional values in plot for the x or y axis instead of decimal values.
For example show 1/298 instead of 3.3557e-3
I know you can manually label the axis but I dont know the end points. Is there a way to do this?
Thanks,
C
  1 commentaire
Image Analyst
Image Analyst le 6 Oct 2012
You can do this to get the existing tick positions, including the end point ticks:
% Get existing tick marks.
existing_Y_Ticks = get(gca, 'YTick')

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 6 Oct 2012
Try this:
% Generate some sample data and plot it.
m = .04 * rand(20, 1);
plot(m);
grid on;
% Now convert the existing tick marks.
% Get existing tick marks.
existing_Y_Ticks = get(gca, 'YTick')
% Define the denominator we want to use.
denominator = 298;
% Find out what the numerators would be.
New_Y_Tick_Numerators = (existing_Y_Ticks * denominator)
% Now make up a cell array of all the tick labels.
for k = 1 : length(existing_Y_Ticks)
y_tick_labels{k} = sprintf('%.1f / %d', New_Y_Tick_Numerators(k), denominator);
end
% Apply our tick marks to the plot.
set(gca,'YTickLabel',y_tick_labels);
  2 commentaires
Walter Roberson
Walter Roberson le 6 Oct 2012
If you do not know the denominator then you may wish to use the function to convert to rational numbers
Image Analyst
Image Analyst le 6 Oct 2012
You mean rats()?
I suppose Mike could also change the tick marks to new values instead of using existing values, so maybe instead of having 5 tick marks with fractional values in the numerator like 1.1/298, 2.2/298, etc. you could have 6 or 7 tick marks with integer multiples of 1/298.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by