0
votes

I am currently trying to make 'nice looking' axis on my plot in Matlab. The values are samples where each is taken every 20ms. So on X axis I'd like to have time in ms. My general code for plotting data is very simple:

plot(1:size( data.matrix,1), data.matrix(:,1),'b');

So far I've been able to correctly display sample numbers on this axis: Code:

set(gca,'XTick',(1:size(data.matrix,1))*20);

Result (a):

enter image description here

In the given example the number of samples equals 184, that makes:

time = 184 * 20ms = 3680ms

Because changing the XTick value didn't do much of a change, I've tested XTickLabel property:

Code:

set(gca,'XTickLabel',ceil(linspace(0,size(data.matrix,1)*20,10)));

Result (b):

enter image description here

Code:

set(gca,'XTickLabel',0:ceil((size(data.matrix,1)*20)/10):size(data.matrix,1)*20);

Result (c):

enter image description here

As you may see, the results aren't the worst one. But this is not what I am trying to achieve. My goal is to have values like this:

enter image description here

So it simply first plot's (result a) X axis multiplied by 20.

Can someone please tell me how to achieve this?

2
Why are you plotting it this way instead of creating a proper time vector? - excaza

2 Answers

2
votes

when you use :

plot(1:size( data.matrix,1), data.matrix(:,1),'b');

The first parameter 1:size( data.matrix,1) create a vector of consecutive integers representing the indices of your data points. Two things are wrong with that:

1) You do not need to specify it. Matlab plot function called with only the Y data will create this vector automatically and use it to plot the data.

2) These indices represent the position of your data in an array, they have no physical meaning (like "time" for your case).

The solution is to create a proper 'time' vector and send it as the first parameter to the plot function. This way Matlab will take care of the tick management and you don't have to temper with the manual xtick settings.

Look at the two ways of doing it in the example below and everything will be clear:

%// build a simple index "X" vector (as you were doing)
x_index = 1:size(data.matrix,1) ;

%// build a properly scaled time vector
timeInterval = 20 ; % in milliseconds
x_time = ( 0:size(data.matrix,1)-1 ) * timeInterval ;

plot( x_index , data.matrix(:,1) ) %// this will do like in your example
plot( x_time  , data.matrix(:,1) ) %// this will do what you want
1
votes

Can you not just do?

set(gca,'XTickLabel',(1:size(data.matrix,1))*20*20);