user1306283
user1306283

Reputation:

Matlab replace axis range

I have x axis from 0 to 96 where every number stands for quarter of hour in a day(96/4 = 24 hours). I need the axis to show the hours 0 to 24 isn't there a way to modify only the axis after the plot?

Upvotes: 2

Views: 1823

Answers (2)

Jonas
Jonas

Reputation: 74940

There are several ways. A good one might be to change the x-data of the plot:

%# get handles of plot objects
chH = get(gca,'children');
%# for each child: divide the x-data by 4 and put it back
if length(chH) == 1
   set(chH,'xdata',get(chH,'xdata')/4);
else
   set(chH,{'xdata'},cellfun(@(x)x/4,get(chH,'xdata'),'uni',0));
end
xlim([0 24])

This reads the x-data of the objects plotted into the current axes, divides it by 4, and puts it back. Then you change the axes limits to 0...24

Upvotes: 1

Roney Michael
Roney Michael

Reputation: 3994

You can use:

>> set(gca, 'XTick', 0:4:96);
>> set(gca, 'XTickLabel', 0:24);

For example:

>> plot(0:96,0:96)
>> set(gca, 'XTick', 0:4:96);
>> set(gca, 'XTickLabel', 0:24);

Resultant figure:

enter image description here

Upvotes: 1

Related Questions