5
votes

I'm trying to plot some data with respect to minutes instead of seconds in Matlab as formatted time, i.e. min.sec.

I have real time data streaming in where with every sample received, its time in seconds is also sent. I then plot them with respect to time. Now, since my session is around 15 minutes long, I can't be plotting with respect to time. Therefore I wanted to plot it with respect to time (min.sec). I tried dividing the received time by 60 but this gives me minutes with 100 subdivisions instead of 60 (the minutes increment after 0.9999 instead of 0.59). How do I convert it so that I'm able to plot with respect to time in minutes?

Here is what I mean by 0.99 fractions of a minute instead of 0.59. A normal minute has 60 divisions not 100.enter image description here

EDIT: I tried m7913d's suggestions and here is what I got.

  1. first I plot the signal with respect to time in seconds without changing the ticks ( A normal plot(t,v))

  2. The I added datetick('x', 'mm:ss'); to the plot (Xtick format is not supported in Matlab 2015b)

Here is a screenshot of the resultsenter image description here

The time in seconds was up to 80 seconds, when translated into minutes, it should give me 1 minutes and 20 seconds as the maximum x axis limit. But this is not the case. I tried to construct a t vector (i.e like t=0:seconds(3):minutes(3)) but I couldn't link it to my seconds vector which will be constantly updating as new samples are received from the serial port. Thanks

1
Edit: Read the question wrong. Can you please post the code that gave you 100 subdivisions instead of 60? Just set your time vector to increment after 60 seconds: t = 0:60:(60*15). - alkasm
My time vector is not set by me, it's recorded from a microcontroller that keeps sending every sample and it's time in seconds. That's why I can't set t=0:60:(60*15). Also, The time duration will be maximum 15 minutes but it could also be less. My current approach is to divide every received time reading by 60, but this gave me the minutes increment every 0.99 fractions of a minute (I'll update the question showing the figure) - Isra
That is the plot view---your samples are not actually lined up with that grid (so your code is right but the view is not what you want). You need to simply change the XTick axis property to put the tick marks where you want and the grid will line up with that. E.g. set(gca,'XTick',start:increment:end);. - alkasm

1 Answers

1
votes

You can use xtickformat to specify the desired format of your x labels as follows:

% generate a random signal (in seconds)
t = 0:5:15*60;
y = rand(size(t));

plot(seconds(t),y) % plot your signal, making it explicit that the t is expressed in seconds
xtickformat('mm:ss') % specify the desired format of the x labels

Note that I used the seconds methods, which returns a duration object, to indicate to Matlab that t is expressed in seconds.

The output of the above script is (the right image is a zoomed version of the left image):

enter image description here enter image description here

Pre R2016b

One can use datetime instead of xtickformat as follows:

datetimes = datetime(0,0,0,0,0,t); % convert seconds to datetime
plot(datetimes,y)
datetick('x', 'MM:SS'); % set the x tick format (Note that you should now use capital M and S in the format string
xlim([min(datetimes) max(datetimes)])