如何在matlab中显示x轴的刻度值

How can I display the scale value in x-axis in matlab

我有一个矢量数据,其中包含 1000 个值,例如

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

我可以使用 plot 在图形中绘制整个数据。然而,它太大了。因此,我对其进行了缩放,以便通过此代码仅在索引 1,5,10....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 到 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.

没问题。只需将您的情节线更改为:

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

这样你就可以告诉它二次采样指数。如果您希望它是 x 值,也没有必要告诉 Matlab XTickLabel,因此将最后两行更改为:

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