Clear Filters
Clear Filters

Using Datenum to convert YYYY MM DD HH

3 views (last 30 days)
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
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)])

Sign in to comment.

Accepted Answer

dpb
dpb on 8 Feb 2017
dNum = datenum([nineteen_49(:,1:4) zeros(length(nineteen_49),2)]);
You missed the second argument to zeros in your augmentation and append two square matrices of 8219x8219 instead of two vectors of length 8219. I'm a little surprised datenum didn't error on the size of the vector 2nd dimension altho I suppose it took it as an extremely precise number of fractions of seconds after the minute, seconds and fractional seconds fields.
  3 Comments
dpb
dpb on 8 Feb 2017
First get rid of all the sequentially-numbered variables and recast the problem in terms of arrays. I presume you must be reading these in from files for the given years, originally? Use dir and iterate over the collection (structure) of file names processing one at a time and creating the end result you need as you go...
There's really no reason to use cell arrays at all in all likelihood; they just add to the complexity in storage and referencing if all is numeric data; cell arrays are good for storing things of disparate types together but that comes at quite a bit of extra complexity and other overhead.
What is the end result you're after; knowing that probably will lead to much more efficient ways to deal with the problems but you're problem really begins with the plethora of variables -- this is NOT the way to use Matlab effectively.
Peter Perkins
Peter Perkins on 8 Feb 2017
I agree with dpb's remarks, but given your date_cell array, it seems like something along these lines ought to do what I think you want, which is "in each of 67 cells, replace the Nx6 matrix with an Nx3 matrix by combining the first four columns into a datenum":
myfun = @(x) [datenum([x(:,1:4) zeros(length(x),2)] x(:,[5 6])];
date_cell = cellfun(myfun,date_cell,'UniformOutput',false)
This uses cellfun, so it may look puzzling, but really it's nothing more than a loop wrapped around dpb's first solution.

Sign in to comment.

More Answers (1)

Peter Perkins
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
Lewis Holden on 8 Feb 2017
Hi Peter, Thanks, but I can't use tables because all my variables have different numbers of rows. I'm rather struggling with how to store it all as a result.
Peter Perkins
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.

Sign in to comment.

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!