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

13 次查看(过去 30 天)
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 个评论
Leyton
Leyton 2024-10-6
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 2024-10-7
编辑:dpb 2024-10-7
@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.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2024-10-6
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 个评论
Leyton
Leyton 2024-10-7
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 2024-10-7
"... 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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by