【问题标题】:How can I display the scale value in x-axis in matlab如何在matlab中显示x轴的刻度值
【发布时间】:2015-11-18 03:18:34
【问题描述】:

我有一个包含 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.

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

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

    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
    

    【讨论】:

    • 感谢 kmac 的帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2017-02-04
    • 2020-05-29
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多