Moon Lee
Moon Lee

Reputation: 395

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

I have a vector data which contains 1000 values such as

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

I can use plot to draw whole data in graph. However, it is too big. Hence, I scaled it so that only take value at index 1,5,10....1000 by this code

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

My problem is the x-axis will be not show actual value from 1 to 1000. It just show 1 to 200 (because 1000:5). I want to show x-axis such as 1:50:1000 for example,

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

How can I do it in matlab? This is my current code

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.

Upvotes: 1

Views: 275

Answers (1)

kmac
kmac

Reputation: 698

No problem. Just change your plot line to:

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

so that you're telling it the subsampled indices. It's also not necessary to tell Matlab the XTickLabel if you want it to be the x-value, so change the last two lines to:

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

There's an easier way to subsample though:

% 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

Upvotes: 2

Related Questions