簡體   English   中英

指定軸的極限

[英]Specifying limits of axes

我試圖在MatLab中將x軸(即頻率軸)限制為4 Hz。 這是我使用的代碼:

        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);

但是我的是實時信號圖,並且x軸和y軸都根據傳入數據包不斷變化。 如何將x軸限制為4 Hz?

繪制時,MATLAB會自動嘗試使軸與數據的動態范圍相適應。 因此,如果要確保僅繪制給定范圍,則需要在調用圖之后指定它,以強制MATLAB執行它,否則它將不起作用,並且整個數據將被您困住。

這是一個非常簡單的代碼,在xlim之前或之后我都調用xlim 看到不同?

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')

產生這個:

在此處輸入圖片說明

您必須將軸的XLimMode (和YLimMode )屬性設置為manual 但是即使這樣做,每次調用plot(...)都會將其重置為auto並破壞軸的限制。

最干凈的方法是先在任何循環之外定義軸和圖(不要忘記獲取它們的句柄),然后在更新數據時,只需使用set方法更新線對象的XDataYData set方法只會更新您傳入參數的屬性,因此不會修改XLimMode屬性。

%// 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 ) ;

您可以使用在那里定義的功能軸軸功能matlab

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM