Converting string cell array to dates
이전 댓글 표시
Hi,
I need to upload a large (100mb)comma separated .dat file in matlab 2008b. The first column of the file represents the date and time (yyyy-mm-dd HH:MM:SS.S). When I use the code below, the data upload fine and the first column comes out as "yyyy-mm-dd HH:MM:SS.S" in the cells. I am trying to convert this to serial dates using datenum? What I've tried so far resulted in this error message:
??? Error using ==> datenum at 174
DATENUM failed.
Error in ==> Load_and_graph_V5 at 6
Is the problem related to the " " at the begining and the end of the date?
datenum(T{1},formatIn)
fid = fopen('TOA5_TGA.RawData_47_2016_11_21_0000.dat', 'r');
T = textscan(fid, '%s%f%f%f%f%f%f%f%f%f%f%f%f%f', 'Delimiter',',','HeaderLines',4);
fclose(fid);
formatIn = 'yyyy-mm-dd HH:MM:SS.S';
datenum(T{1},formatIn)
Any help or suggestions would be appreciated!
Thanks
댓글 수: 13
per isakson
2016년 11월 24일
Try
formatIn = 'yyyy-mm-dd HH:MM:SS.FFF';
Luc
2016년 11월 25일
dpb
2016년 11월 25일
Yeah, show us a small subset of the actual data and how it is stored instead of just describing it; including whatever it is that made you make the comment regarding quotes. Yes, if the date string does include a double-quote you'll have to include it in the formatting string or remove it; if it's simply a single quote (') surrounding it when displayed at the command line for a value, that's just a Matlab display convention for cellstr and isn't part of the string itself.
But, we can't see your terminal from here so we don't know...
per isakson
2016년 11월 25일
편집: per isakson
2016년 11월 25일
"Any other idea??"  
- "FFF, Millisecond in three digits"   might not be available in R2008b. Check the documentation.
- Your data string doesn't honor the format you claim.
with R2016a
>> sdn = datenum( '2016-11-25 16:15:39.4', 'yyyy-mm-dd HH:MM:SS.FFF' )
sdn =
7.366596775393519e+05
>> datestr( sdn, 'yyyy-mm-dd HH:MM:SS.FFF' )
ans =
2016-11-25 16:15:39.400
"FFF, Millisecond in three digits" might not be available in R2008b."
It's in R14 which predates R2008X by quite some time...
>> datestr(datenum( '2016-11-25 16:15:39.4', 'yyyy-mm-dd HH:MM:SS.FFF' ), 'yyyy-mm-dd HH:MM:SS.FFF')
ans =
2016-11-25 16:15:39.400
>> version
ans =
7.1.0.246 (R14) Service Pack 3
>>
I don't have anything between R14 and R2012b, but surely can't imagine it retrogressed in between...
I think there's some other formatting problem going on here we can't see because don't have the actual file or data from the file which is your second alternative above--pretty sure that's the issue; question is just how that we can't tell w/o the actual data instead of just a description. (As so often the case, that can't see the problem means good chance OP doesn't recognize how that is so, otherwise would've fixed it).
per isakson
2016년 11월 25일
- That rules out "not available in R2008b"
- "I just tried this and I get the following error message:"   Tried what?   ":SS.S" makes me unsure.
- Another idea: Corrupt line in the middle of the file. "large (100mb)comma separated .dat file"
dpb
2016년 11월 25일
Indeed, OP is markedly short on detail; since the comment followed your suggestion regarding .FFF instead of .S, one would presume that's what he/she did, but no way to know for certain.
The latter is also quite possible; this one doesn't have the particular symptom but I've run into it with the AM/PM form where one or more instances may be missing it which causes just such a failure.
I've beefed to TMW before on the lack of help the error from datenum provides; it could at least echo the string it's trying to convert at the time of failure it would seem...
Luc
2016년 11월 29일
dpb
2016년 11월 29일
One pretty effective technique is to use bisection to locate the position within the file that is the culprit. Start by saving first half (roughly, doesn't have to be exact, exact) and see if that fails/succeeds. Rinse and repeat on the section that does fail and you'll shortly have a small enough section to isolate the offending line.
Luc
2016년 11월 29일
편집: Walter Roberson
2016년 11월 29일
Walter Roberson
2016년 11월 29일
NewCell = regexprep(YourCell, '(:\d\d)$', '$1.0', 'lineanchors')
Luc
2016년 11월 30일
dpb
2016년 11월 30일
I'll just note that perhaps could fix the problem in the place where the file was created initially to ensure consistent format.
I've bit^h^h^h complained to TMW previously over how fragile the time conversion format parsing is--it seems to me there should be no reason for it to fail there; the value is unambiguous. It'd be a reasonable enhancement request to suggest it not fail or, if that really is difficult owing to the way the C-based i/o formatting strings function, at least echo the line that caused the problem in the error.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!