- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do? Be specific.
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
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 个评论
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
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 个评论
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 Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!