2015-11-18 101 views
1

顯示在x軸上的刻度值I有一個包含1000個值如我怎樣才能在MATLAB

data=[1,2,...1000] 

我可以使用情節圖形繪製整個數據的矢量數據。但是,它太大了。因此,我縮放它,以便通過該代碼

index=0; 
for I=1:5:1000 
    index=index+1; 
    data_scale(index)=data(i); 
end 
plot(1:length(data_scale),data_scale); 

我的問題是x軸將不顯示實際值從1至1000只需要在索引1,5,10 .... 1000值。它只顯示1到200(因爲1000:5)。我想顯示x軸,例如1:50:1000,例如,

y_axis=[data(1), data(5),data(10)] 
Corresponding to 
x_axis=[1   50  100 ] 

我該如何在matlab中做到這一點? 這是我現在的代碼

index=0; 
labels=[]; 
data_scale(1)=data(1) 
for i=1:1:1000 
    if(rem(i,5)==0) 
     index=index+1; 
     data_scale(index)=data(i); 
     if(rem(i,50)==0) 
     labels=[labels i]; 
     end 
    end 
end 
plot(1:length(data_scale),data_scale); 
set(gca, 'XTick', 1:length(labels),'FontSize', 12); % Change x-axis ticks 
set(gca, 'XTickLabel', labels); % Change x-axis ticks labels. 

回答

2

沒問題。只需將您的劇情線改爲:

plot(1:5:length(data_scale),data_scale); 

以便您告訴它二次採樣索引。它也沒有必要告訴MATLAB的XTickLabel如果你希望它是x值,所以最後兩行更改爲:

set(gca, 'XTick', labels,'FontSize', 12); % Change x-axis ticks 

有來,雖然二次採樣更簡單的方法:

% SUBSAMPLE This is probably all you need 
x = 1:5:length(data); % The indices you want to use 
data_scale = data(x); % The subsampled data (this is called slicing) 
plot(x, data_scale); % Plot 

% OPTIONAL If you want to control the label positions manually 
labels = 1:50:length(data); % The labels you want to see 
set(gca, 'XTick', labels, 'FontSize', 12); % Optionally specify label position 
+0

謝謝kmac爲你提供幫助。 –