Datetime/Duration Error: Input data must be one numeric matrix when converting from a different date/time representation.

6 views (last 30 days)
Since a previous post's solution with the same datetime error didn't work on my end, I made this post.
I am trying to combine Date and Time variables by datetime and duration them respectively from a 1454x7 table labelled "buoyF".
buoyF = renamevars(buoyF, {'Var1', 'Var2', 'Var3', 'Var4', 'Var5', 'Var6', 'Var7'}, ...
{'Date', 'Time', 'East', 'North', 'Speed', 'Water Direction', 'Temp'});
buoyF.Date = datetime(buoyF.Date, 'ConvertFrom', text, 'Format', 'dd/MM/yyyy');
buoyF.Time = duration(buoyF.Time,'ConvertFrom', text, 'Format', 'hh:mm:ss');
buoyF.datetime = buoyF.Date+buoyF.Time
  3 Comments
Leyton
Leyton on 6 Oct 2024
Received Error Messages when I ran the above code:
Error using datetime
Input data must be one numeric matrix when converting from a different date/time representation.
dpb
dpb on 7 Oct 2024
Edited: dpb on 7 Oct 2024
@Steven Lord, the error message/input preprocessing in especially duration might be improved; this took a lot longer to diagnose the problem because the error messages were all about trying to convert the string representation failing whereas the underlying problem was that the inputs already were datenum and duration because the readxxx routines automagically and silently interpreted the input and returned datetime dates and duration times.
If the input types were checked first; the error could be returned of "Cannot convert Duration class variable as character input" or somesuch...or, one could even go so far as to turn the error into a warning and just return the input data.
Either way, the actual error information produced was not particularly useful in identifying the root cause.

Sign in to comment.

Accepted Answer

dpb
dpb on 6 Oct 2024
buoyF = renamevars(buoyF, {'Var1', 'Var2', 'Var3', 'Var4', 'Var5', 'Var6', 'Var7'}, ...
{'Date', 'Time', 'East', 'North', 'Speed', 'Water Direction', 'Temp'});
buoyF.Date=datetime(buoyF.Date,'InputFormat','dd/MM/yyyy');
buoyF.Time=duration(buoyF.Time,'InputFormat','hh:mm:ss');
buoyF.DateTime = buoyF.Date+buoyF.Time;
You aren't converting from another serial date format representation but reading from a text string.
Of course, as the other recent thread Answer showed, the format string has to be consistent with the actual date and time strings being read which you forgot to show here so we can't tell about whether that is correct for the given case or not.
In general, if I were going to convert the separate date and time fields together into a datetime variable, I'd do the following
buoyF.Date=datetime(buoyF.Date,'InputFormat','dd/MM/yyyy')+duration(buoyF.Time,'InputFormat','hh:mm:ss');
buoyF=removevars(buoyF,'Time');
to be more parsimonious and keeping the shorter 'Date' as the variable name. If the subsequent usage were to be more convenient to having a separate date and time variable, then I'd probably not bother to create the combined (but I can't otomh think of a particular reason this would likely be true). Then again, in today's larger memory footprints, unless the table were really, really big, the extra memory probably doesn't matter...
  9 Comments
Leyton
Leyton on 7 Oct 2024
Makes a lot of sense! When I was given example code, I was confused to why I would need to read the variables twice but could not combine "buoyF.Date" and "buoyF.Time" because the "." is not recognized but adding this table with the code and changing "buoyF" to "tbF" seems to do the trick.
Thank you! I greatly appreciate your help today!
dpb
dpb on 7 Oct 2024
"... changing "buoyF" to "tbF" seems to do the trick."
I just used a shorter variable name; that has nothing at all to do with whether the code runs or not...my penchant like with variables in the table we talked about earlier is to use as brief a name as can that makes it easy to recall what is what--long, drug-out names just require more typing and make code more difficult to read. The compiler doesn't care, so make it as simple for yourself as possible.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!