필터 지우기
필터 지우기

Using Datenum to convert YYYY MM DD HH

조회 수: 3 (최근 30일)
Lewis Holden
Lewis Holden 2017년 2월 8일
댓글: Peter Perkins 2017년 2월 8일
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
Stephen23
Stephen23 2017년 2월 8일
There is no need to write such complicated code, much simpler is:
dNum = datenum([nineteen_49(:,1:4),zeros(size(nineteen_49,1),2)])

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

채택된 답변

dpb
dpb 2017년 2월 8일
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
dpb
dpb 2017년 2월 8일
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 2017년 2월 8일
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.

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

추가 답변 (1개)

Peter Perkins
Peter Perkins 2017년 2월 8일
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
Lewis Holden
Lewis Holden 2017년 2월 8일
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 2017년 2월 8일
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.

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by