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

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

What does "didn't work" mean in this context?
  • 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.
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.
@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.

댓글을 달려면 로그인하십시오.

 채택된 답변

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

I greatly appreciate your help and explanations behind your thinking!
I had thought about writing the date and time format but for some reason left it out on the question.
Here is a preview of my table:
Just got a new error message on duration for:
buoyF.Date=datetime(buoyF.Date,'InputFormat','MM/dd/yyyy')+duration(buoyF.Time,'InputFormat','hh:mm:ss');
That says
Unrecognized function or variable 'convertFromText'.
Error in duration
if ~convertFromText
The format strings look ok with the exception we can't tell whether the time portion is 12 or 24 hour time -- the datetime documentation links to the <All Date and Time Formats> table in which you'll see that "hh" is two-digit 12-hour clock while "HH" is for 24-hour clock. It's ambiguous from the data you've provided(*) and while common formats such as the above will normally be parsed automagically if you don't actually specify the input format, if you do specify the format it much match identically.
(*) Although if it were a 12-hr clock, then one would need the not-shown AM/PM suffix so one can presume you really need to use
buoyF.Date=datetime(buoyF.Date,'InputFormat','dd/MM/yyyy')+duration(buoyF.Time,'InputFormat','HH:mm:ss');
The table time is within the 24-hour clock so I swapped out the "hh" with "HH". However, it doesn't stop the error of the current duration error
Oh...I keep forgetting that duration doesn't use the same convention for its input text string formatting -- 'HH' is not valid for them but only for the clock times for datetimes...use the 'hh:mm:ss" for the duration.
Sorry, that was a bum steer...
tT=cell2table([{'01/Jan/08';'01/Jan/08'} {'00:00:00';'03:00:00'}],'VariableNames',{'Date','Time'})
tT = 2x2 table
Date Time _____________ ____________ {'01/Jan/08'} {'00:00:00'} {'01/Jan/08'} {'03:00:00'}
tT.Date=datetime(tT.Date,'InputFormat','dd/MMM/yy')+duration(tT.Time,'InputFormat','hh:mm:ss')
tT = 2x2 table
Date Time ____________________ ____________ 01-Jan-2008 00:00:00 {'00:00:00'} 01-Jan-2008 03:00:00 {'03:00:00'}
tT=removevars(tT,'Time')
tT = 2x1 table
Date ____________________ 01-Jan-2008 00:00:00 01-Jan-2008 03:00:00
should simulate your input file/results...
If using 'hh:mm:ss" for the duration column still errors we'll need to have the actual file data to diagnose any further I expect; that then sounds like something in the file isn't what you think it is, but all we can see looks ok...
Just ran the example code and it works but my changing the duration column did not remove any errors. I sent my txtfile to check.
L=readlines('TABS_buoyF_sep2008.txt');
L(1:5)
ans = 5x1 string array
"09/13/2008 00:00:00 -67.99 -44.99 81.53 236.5 28.3" "09/13/2008 00:30:00 -61.40 -47.75 77.78 232.1 28.3" "09/13/2008 01:00:00 -59.41 -51.97 78.93 228.8 28.3" "09/13/2008 01:30:00 -58.02 -58.58 82.45 224.7 28.3" "09/13/2008 02:00:00 -54.24 -62.50 82.75 221.0 28.3"
nnz(L{1}==9)
ans = 0
tbF=readtable('TABS_buoyF_sep2008.txt');
head(tbF)
Var1 Var2 Var3 Var4 Var5 Var6 Var7 __________ ________ _______ ______ ______ _____ ____ 09/13/2008 00:00:00 -67.99 -44.99 81.53 236.5 28.3 09/13/2008 00:30:00 -61.4 -47.75 77.78 232.1 28.3 09/13/2008 01:00:00 -59.41 -51.97 78.93 228.8 28.3 09/13/2008 01:30:00 -58.02 -58.58 82.45 224.7 28.3 09/13/2008 02:00:00 -54.24 -62.5 82.75 221 28.3 09/13/2008 02:30:00 -59.88 -59.17 84.18 225.3 28.2 09/13/2008 03:00:00 -108.25 23.84 110.84 282.4 28.3 09/13/2008 03:30:00 -60.95 -92.22 110.54 213.5 28.3
tbF=renamevars(tbF, {'Var1', 'Var2', 'Var3', 'Var4', 'Var5', 'Var6', 'Var7'}, ...
{'Date', 'Time', 'East', 'North', 'Speed', 'Direction', 'Temp'});
tbF.Date=tbF.Date+tbF.Time;
tbF=removevars(tbF,'Time');
tbF.Date.Format='default';
head(tbF)
Date East North Speed Direction Temp ____________________ _______ ______ ______ _________ ____ 13-Sep-2008 00:00:00 -67.99 -44.99 81.53 236.5 28.3 13-Sep-2008 00:30:00 -61.4 -47.75 77.78 232.1 28.3 13-Sep-2008 01:00:00 -59.41 -51.97 78.93 228.8 28.3 13-Sep-2008 01:30:00 -58.02 -58.58 82.45 224.7 28.3 13-Sep-2008 02:00:00 -54.24 -62.5 82.75 221 28.3 13-Sep-2008 02:30:00 -59.88 -59.17 84.18 225.3 28.2 13-Sep-2008 03:00:00 -108.25 23.84 110.84 282.4 28.3 13-Sep-2008 03:30:00 -60.95 -92.22 110.54 213.5 28.3
The problem is that the input routine already recognized them as dates and times and read in as a datetime and duration so there's no need to convert from text; I was presuming the input data were string or cellstr.
So, all you have to do is just go ahead with the existing two variables Date, Time or go ahead and combine into the one; your choice; as noted, if you're plotting time series and the like it certainly makes sense to combine to get the full UTC time for those purposes.
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!
"... 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개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

릴리스

R2023a

질문:

2024년 10월 6일

편집:

dpb
2024년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by