CaptainProg
CaptainProg

Reputation: 5690

Rescaling axes without zooming

I'd like to rescale the axis of a MATLAB plot without modifying the underlying data. I'm not trying to zoom in on a particular region of the plot.

As an example, lets say I have my X axis in millimetres. My American colleagues might prefer to see the output in inches, but everything is coded in millimetres, and it'd be a hell of a job to create new inch-based data for all the items I'd like to plot. Ideally, I'd just plot everything as usual, and in a couple of lines of code, have the X-axis adjust to inches at the end.

How do I do this?

Basic code to get things started:

plot([1:2:100], [1:50])
xlabel('Millimetres')
ylabel('Something else')

% Magic happens

xlabel('Inches')

Note: 1 inch is 25.4 millimetres.

Upvotes: 0

Views: 153

Answers (1)

bla
bla

Reputation: 26069

First, what's the problem dividing your x-data by 25.4 ?

x=[1:2:100]; y=[1:50];
plot(x/25.4,y)

will do. This will also automatically place the X-Ticks positions and Labels in a nice round number positions.

If you insist, this will convert the current X-Tick Labels from mm to inch units:

xt     = get(gca, 'XTick');
xlabels= get(gca, 'XTickLabel');
set(gca, 'XTick', xt, 'XTickLabel',num2str(str2num(xlabels)/25.4) );

Upvotes: 1

Related Questions