Using Datenum to convert YYYY MM DD HH
Show older comments
I have an 8219x6 matrix containing YYYY MM DD HH Wind_Speed Wind_Dir
I am trying to convert the first 4 columns using datenum using the code:
dNum = datenum([nineteen_49(:,1) nineteen_49(:,2) nineteen_49(:,3) nineteen_49(:,4) zeros(length(nineteen_49(:,4))) zeros(length(nineteen_49(:,4)))]);
But this isnt working as it is returning an 8219x16422 matrix instead of 8219x6. Not sure what the problem is?
1 Comment
Stephen23
on 8 Feb 2017
There is no need to write such complicated code, much simpler is:
dNum = datenum([nineteen_49(:,1:4),zeros(size(nineteen_49,1),2)])
Accepted Answer
More Answers (1)
Peter Perkins
on 8 Feb 2017
Lewis, if you're using a recent version of MATLAB, take a look at datetime and tables. Maybe even timetables if you have R2016b. To create a datetime vector, this will do it:
d = datetime(nineteen_49(:,1:3) + hours(nineteen_49(:,4));
and then
t = table(d,nineteen_49(:,5),nineteen_49(:,6), ...
'VariableNames',{'Time' 'WindSpeed' 'WindDirection'})
2 Comments
Lewis Holden
on 8 Feb 2017
Peter Perkins
on 8 Feb 2017
I'm not 100% sure I understand. nineteen_49 is a double matrix, and the table I'm suggesting that you create uses columns of that matrix, which all necessarily have the same number of rows. I guess you have a more complicated picture than that, with 67 matrices that are "like" nineteen_49 but with different numbers of rows, and you're trying to put all 67 of those inside one hierarchical container.
One alternative strategy is to create one Mx4 table that contains all your data, with variables for date/time, wind speed, and wind direction, as well as a year variable. The latter can be used to conveniently pick out one particular year's worth of data.
Another possibility is to do what you're doing with a 67x1 cell array, but put a table in each cell, not a double matrix. Or, for that matter, a 67x2 table with a year variable and a cell array variable, each cell of which contains another year-specific table.
It's hard to say for sure what's a good way to organize your data, because it's not clear what you're doing with it.
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!