Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to create empty datetime array?

2,368 views
Skip to first unread message

Bruce Elliott

unread,
Dec 4, 2014, 11:15:12 AM12/4/14
to
I am working on a GUI that will allow users to enter (optionally) a list of times. I'm storing the times in a datetime array, so I'd like to be able to initialize it as a zero-length array so that functions can work with it whether or not it has been populated with times.

I tried simply setting

myTimes = [];

but this initializes myTimes as a double array, so I get an error when I later try:

myTimes(1) = datetime(inputs);

As a work-around I can neglect to initialize the array and then force all functions that use it to check first to see if it exists, but that seems cumbersome.

Is there a way to initialize an empty datetime array?

Thanks.

Bruce Elliott

unread,
Dec 4, 2014, 11:25:12 AM12/4/14
to
"Bruce Elliott" <bruce....@jhuapl.nospam.edu> wrote in message <m5q1ab$eo3$1...@newscl01ah.mathworks.com>...
Dang! I keep finding the answer to my own post within minutes of posting it, no matter how long I had spent trying to find it before posting. I swear I'm not doing it on purpose!

Anyway, this works:

>> datetime([],[],[])

ans =

Empty datetime matrix: 0-by-0

Got it.

Eric Sampson

unread,
Dec 4, 2014, 11:32:11 AM12/4/14
to
"Bruce Elliott" <bruce....@jhuapl.nospam.edu> wrote in message <m5q1ab$eo3$1...@newscl01ah.mathworks.com>...
Hi Bruce, try one of these:

>> t = datetime(zeros(0,3))
t =
Empty datetime matrix: 0-by-1
>> t = datetime(double.empty(0,3))
t =
Empty datetime matrix: 0-by-1

dpb

unread,
Dec 4, 2014, 11:39:56 AM12/4/14
to
Don't have release late enough to include it but from the online doc's
doesn't appear TMW thought of that -- unless

dt=datetime([]);

will work but I suspect it, too, will error. As said, can't test it here.

Submit an enhancement request; the query should be sufficient "why" and
"how I would use it" for the justification part.

Presuming the above doesn't work--maybe you can figure a way to factor
your app so that only need to call the exist() function once or at least
a relatively few number of times and then can make most functions only
get called when you know it does exist.

If that doesn't work, you can save some execution time for another
logical variable that you keep that is set that is T IFF the object has
been created and not yet destroyed. This would need to be a GLOBAL or a
property of a structure or somesuch so it's always available, of course.
But,

if ~dtExist, dt=datetime(whatever); dtExist=T; end

is probably noticeably faster in timing altho no less coding than

if ~exist('dt','class'), ....

I guess the other workaround would be to created the object but use a
default dummy value for the start time although one hates to rely on
magic numbers as a general rule. What happens if you try a Datevector
of zeros, for instance? Does it "blow up" or give a nonsensical but
consistent result that can test?

--

Eric Sampson

unread,
Dec 4, 2014, 11:40:11 AM12/4/14
to
"Bruce Elliott" <bruce....@jhuapl.nospam.edu> wrote in message <m5q1t2$gar$1...@newscl01ah.mathworks.com>...
>
> Dang! I keep finding the answer to my own post within minutes of posting it, no matter how long I had spent trying to find it before posting. I swear I'm not doing it on purpose!
>

See http://www.rubberduckdebugging.com/ :D

Bruce Elliott

unread,
Dec 4, 2014, 12:11:10 PM12/4/14
to
"dpb" wrote in message <m5q2o6$4hb$1...@dont-email.me>...
>
> Don't have release late enough to include it but from the online doc's
> doesn't appear TMW thought of that -- unless
>
> dt=datetime([]);
>
> will work but I suspect it, too, will error. As said, can't test it here.
>
> < snip ...>

I suspect you had not seen the subsequent replies (including my own) when you wrote this.

I had in fact tried dt = datetime([]) first, but sure enough, that throws an error. I should have gotten a clue, however, from the fact that the error complained only about the number of input arguments.

As above, this works:

dt = datetime([],[],[]);

Bruce Elliott

unread,
Dec 4, 2014, 12:12:11 PM12/4/14
to
"Eric Sampson" wrote in message <m5q2p6$ioe$1...@newscl01ah.mathworks.com>...
> "Bruce Elliott" <bruce....@jhuapl.nospam.edu> wrote in message <m5q1t2$gar$1...@newscl01ah.mathworks.com>...
> >
> > Dang! I keep finding the answer to my own post within minutes of posting it, no matter how long I had spent trying to find it before posting. I swear I'm not doing it on purpose!
> >
>
> See http://www.rubberduckdebugging.com/ :D

Busted.

In fact, the MATLAB Newsgroup IS my rubber duck. ;-)

I'll look into acquiring a real one ...

Steve Eddins

unread,
Dec 4, 2014, 12:29:21 PM12/4/14
to
Hilarious! And so true!

Steve Eddins

unread,
Dec 4, 2014, 12:39:56 PM12/4/14
to
There are some advantages to having a real one. They are definitely more
brightly colored than comp.soft-sys.matlab. Taking the newsgroup with
you into the bathtub can be problematic. And real rubber ducks often
squeak, something that the newsgroup almost never does.

Steven Lord

unread,
Dec 4, 2014, 1:20:38 PM12/4/14
to


"Steve Eddins" <Steve....@mathworks.com> wrote in message
news:m5q696$s5e$1...@newscl01ah.mathworks.com...
A plush Peeps toy like this one looks kind of like a duck (or at least a
rubber ducky) though unlike either a real duck or a rubber duck it never
lets out a peep. :)

http://www.peepsandcompany.com/product/plush-santa-peeps-chick

Squeezing it can relieve stress. Just don't try to eat it like a real Peep.
And I wouldn't put it in the microwave.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Bruce Elliott

unread,
Dec 4, 2014, 2:11:15 PM12/4/14
to
"Steven Lord" <Steve...@mathworks.com> wrote in message <m5q8lc$5tk$1...@newscl01ah.mathworks.com>...
I've said it before, and I'll say it again - you get advice on the Mathworks newgroup that you just won't find anywhere else.

Richard Ellis

unread,
Dec 4, 2014, 3:36:57 PM12/4/14
to


"Bruce Elliott" <bruce....@jhuapl.nospam.edu> wrote in message
news:m5q4j9$npc$1...@newscl01ah.mathworks.com...
This is also a solution:

>> dt = datetime.empty

dt =

Empty datetime matrix: 0-by-0

Bruce Elliott

unread,
Dec 4, 2014, 4:14:11 PM12/4/14
to
"Richard Ellis" <Richar...@mathworks.com> wrote in message <m5qgl3$s6p$1...@newscl01ah.mathworks.com>...
Thanks - that's more the kind of thing I was looking for. I don't know if it's preferable to the other suggested approaches for any reason, but it just seemed like there ought to be a special method for creating empty datetime arrays.

Bogdan Batrinca

unread,
Oct 15, 2015, 5:03:10 AM10/15/15
to
Initialising an empty datetime array with datetime([], [], []) works perfectly. Another method is to initialise the datetime array with today's date and then remove this first element, which causes the datetime array to become empty.

a = datetime;
a(1) = [];
0 new messages