Plotting time series with NaN?

I am using time series to plot temperature every 10 minutes over the course of a month. The only problem I have is that when I run it, the increments are every minute, as determined by the units. I'm not sure how to use the TimeInfo.Increment coding, considering my t array contains NaN. This is the code I have
ts1 = timeseries(t,1:4032); ts1.Name = 'Temperature vs Time'; ts1.TimeInfo.Units = 'minutes'; ts1.TimeInfo.StartDate='01-Feb-2011'; % Set start date. ts1.TimeInfo.Format = 'dd, HH:MM'; % Set format for display on x-axis. ts1.Time=ts1.Time-ts1.Time(1); % Express time relative to the start date. ts1.TimeInfo.Increment='1/144'; plot(ts1)
and I am getting an error:
Operands to the and && operators must be convertible to logical scalar values.
Error in tsdata.timemetadata/set.Increment (line 168) if ~isempty(input) && isfinite(input) && ~isempty(this.Time_)
Error in att201102 (line 63) ts1.TimeInfo.Increment='1/144';
Before I tried using this Increment code, it plotted the correct points but with a wrongly labeled x-axis (every minute instead of every 10 minutes).I have a feeling the NaN in my t array is the cause of this. Thanks

 Accepted Answer

dpb
dpb on 7 Oct 2013
Edited: dpb on 8 Oct 2013
Before I tried using this Increment code, it plotted the correct points but with a wrongly labeled x-axis (every minute instead of every 10 minutes).I have a feeling the NaN in my t array is the cause of this
You may "have a feeling" but you'd be wrong... :)
plot very nicely just ignores NaN leaving holes in the plot.
If you got one-minute increments, that's because the X-vector is undoubtedly on a range of 1:N instead of actually in minutes.
As I said in answering your previous question on the same subject, if you want a time axis w/ date/time, then the easiest is to convert to datenums and use datetick.
If, otoh, the data are at 10-minute intervals and are none missing (or the missing locations in the time vector are filled w/ NaN), and you want to plot from 0 to length of the data series on a 10 minute interval, just use
t=[0:10:10*(length(x)-1)];
which is, of course, just
t=10*[0:length(x)-1];
for the plot x-axis vector. IOW, build the 10-minute interval into the data.
Or, use 1:length(x) and reset the x-tick labels but this is more work than simply making the time vector and using it.

4 Comments

I'm not sure that I can use dateNum, as my time is in one array, in HH:MM UTC format.As for your other code, is 'x' referring to the x-axis? And t referring the y-axis variable? Inputting this gives me an error as x is undefined. Also, I should've clarified; I did not use the actual array with values for time, I assumed time series would be able to take a start date and unit increment and be able to fill the plot.
dpb
dpb on 8 Oct 2013
Edited: dpb on 8 Oct 2013
a) "x" is just a variable name standing for the variable you wish to plot--it's simply demonstrating using the length() of that vector/array you wish to plot to generate an independent time axis.
b) Of course you can use datenums; all you have to do is to take the specific format you have and convert as needed. There's still not enough detail specified in your response to know the precise format to provide any specifics, but as long as there's no ambiguity in the data there's no fundamental problem.
Now, whether that's the best solution or not depends on the question posed above as to what it is you want on the x-axis labels -- calendar dates or just 0:N minutes from some origin to however minutes there are in the length of data. Again, you've not specified that.
c) I've never used the time series object but have looked at the documentation just a little. I think you're assuming more in it than there is to the best of my recollection. Oh, ok, a quick glance again shows it's dependent on how you created the time series -- but the default is as I suspected simply the vector [0:N-1]
I've responded mostly w/ the idea of using the raw data and the plot function but it appears that if you were to create the ts object with a time of 10*[0:N-1] that the .plot method for a ts object would do it. But, you'll have to set that time vector when you create the object it appears; otherwise you'll get what you have--a 1-interval vector of whatever units you set.
ADDENDUM:
Given the last; if you create the default time series by storing the data, then it appears you could simply
ts.time=10*ts.time;
to scale to the 10-minute intervals given the default of [0:N-1].
ok great! This is finally starting to work! Thanks so much!! It took a bit of playing around, but adding that last line of code, ts1.time=10*ts1.time and removing the line that I had earlier (ts1.time=...) did the trick. This should make the rest of my work go much smoother (hopefully) - so thank you!
Yes. It seems like it should be so a ts object could be specified w/ a constant dt and units instead of requiring the explicit time vector but that isn't what was implemented.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 7 Oct 2013

Commented:

dpb
on 9 Oct 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!