How to change matlab x-axis on a subplot

10 views (last 30 days)
Filip Krähenbühl
Filip Krähenbühl on 23 Oct 2017
Edited: Cam Salzberger on 25 Oct 2017
This is a extract of my m-file. The code below generates a plot, but the x-axis just wont adjust how I want it. No matter what I try. What's wrong? (I want to replace enumeration, which is from 1 to 20732 with the actual time which should be from 0 to 41.4640 seconds)
And, yes, I'd like to have it in a subplot... also tried to add second x-axis. Sounds good, doesn't work.
figure('Name','Respiration at rest');
fs=500;
stot=(1/fs*20732);
saxis = linspace(1,stot,20732)';
ax1=subplot(2,1,1), plot(ax1,saxis,data_5(:,1))
axis([ax1 ax1],[1 stot -2 4])
xlim(ax1,[1 stot]);

Answers (2)

Cam Salzberger
Cam Salzberger on 23 Oct 2017
Hello Filip,
When I run the code, the axes is shrunk to the size of a subplot. If you are trying to create a second subplot, you can use something like:
ax2 = subplot(2, 1, 2);
Using axis([ax1 ax1], ...) doesn't do anything different than axis(ax1, ...). It just changes the x and y limits for the same axes (ax1) twice.
It seems like you would like the data to be plotted against the "saxis" values (1 to 20732), but you want the tick marks to show up as different values. You can do this by setting the 'XTick" and 'XTickLabel' properties of the axes.
Remember that the underlying data units will still remain the same, so I'd remove the call to "xlim" and "axis", since you're limiting the window to not display the full range of data. Unless that's what you want, of course.
-Cam

Filip Krähenbühl
Filip Krähenbühl on 24 Oct 2017
Ok, guess I have to clarify. What I want is to adjust the x-axis-scale.
data_5 = load('5.data');
figure('Name','Respiration at rest');
[M,N] = size(data_5);
x_data_5=(1:M)';%samples-enumeration
fs=500;%Sampling Rate [Hz] (taken from header)
stot=(1/fs*M);%total-Sampling time [s]
saxis = linspace(0,stot,M)';%x-axis in seconds
%ax1=subplot(2,1,1), plot(x_data_5,data_5(:,1));
ax1=subplot(2,1,1); plot(ax1,saxis,data_5(:,1))
When I don't do it as a subplot, there are no issues...
figure(8)
plot(saxis,data_5(:,1))
First extract gives me figure1, and seconde figure 2
  2 Comments
Cam Salzberger
Cam Salzberger on 24 Oct 2017
Edited: Cam Salzberger on 25 Oct 2017
Hello Filip,
Typically, if responding to an answer, we post a comment under it, rather than a new answer.
I ran the code on some random data:
data_5 = sin(linspace(0, 40*pi, 20000)).';
and saw this for the subplot:
This matched what I saw for the x-axis when run with your second bit of code:
However, when I ran your commented-out bit of code, that uses x_data_5 as the x-values, rather than saxis, that's when I saw your "differently-scaled" x-axis:
Just be careful which data you are plotting.
-Cam
Filip Krähenbühl
Filip Krähenbühl on 24 Oct 2017
I got it now! It had nothing to to with the extract I posted! A bit below I used
findpeaks(data_5(:,1),'MinPeakDistance',0.1*10^4); %find peaks
[pks1,locs1]=findpeaks(data_5(:,1),'MinPeakDistance',0.1*10^4);%save peaks
and this apparently readjusts the scale.
Thank you anyway

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!