当前位置: 动力学知识库 > 问答 > 编程问答 >

matlab - Specifying limits of axes

问题描述:

I'm trying to limit x axis, i.e., frequency axis to 4 Hz in MatLab. This is the code I used:

 subplot(3,1,2);

%Fse = 220;

time = 0:1/fse:secBuffer-1/fse;

%a = eegCounter;

c = eegBuffer;

wo = 50 / (1000/2);

bw = wo / 60;

[b,a] = iirnotch(wo,bw);

y = filter(b,a,c);

ydft = fft(c);

xdft = fft(y);

xlabel('Frequency');

ylabel('Signal');

xlim([1,4]);

ylim([1,4]);

plot(xdft,ydft);

However mine is live signal plotting and both x axis and y axis keep changing according to incoming packets. How to limit x axis to 4 Hz?

网友答案:

When plotting MATLAB automatically tries to fit the axis with the dynamic range of the data. Therefore if you want to make sure only a given range is plotted, you need to specify it AFTER the call to plot to force MATLAB to do it, otherwise it won't and you will be stuck with the whole data.

Here is a very simple code in which I call xlim either before or after the call to plot. See the difference?

clear
clc
close all

x = 1:50;
y = x.^2;

figure
subplot(1,2,1)
xlim([1 20])
plot(x,y)
title('xlim before call to plot')

subplot(1,2,2)
plot(x,y)
xlim([1 20])
title('xlim after call to plot')

Produces this:

网友答案:

You have to set the XLimMode (and YLimMode) properties of the axes to manual. But even if you do so every call to plot(...) will reset that to auto and mess up your axes limits.

The cleanest way is to first define your axes and your plots outside of any loop (not forgetting to get their handle), then when you update the data just update the XData and YData of the line objects, using the set method. The set method will only update the property you pass in parameters, so it will not modify the XLimMode property.

%// This part of the code should run only once
h.ax = subplot(3,1,2) ; %// get the handle of the axes
h.line = plot(0) ;    %// create an empty line plot
set(h.ax , 'XLimMode','manual' , 'XLim',[1 4]) ; %// define the properties of the axes (X)
set(h.ax , 'YLimMode','manual' , 'YLim',[1 4]) ; %// define the properties of the axes (Y)
xlabel('Frequency');
ylabel('Signal');
%//
%// This part of the code is the loop where you calculate and update your plot
%// ...
%// now do your calculations
%// ...
%// when it is time to update, just call:
set( h.line, 'XData',xdft 'YData',ydft ) ;
网友答案:

You can use the function axis as defined there axis function matlab

分享给朋友:
您可能感兴趣的文章:
随机阅读: