Convert SQL dates to MATLAB datenums

조회 수: 6 (최근 30일)
Sarah
Sarah 2014년 10월 27일
답변: Sarah 2014년 10월 29일
I'm new to the database toolbox and cannot figure out how to convert SQL dates to MATLAB datenums. In the past, I've been able to do the following for converting Excel dates to MATLAB datenums:
[~, ~, raw, dateNums] = xlsread('path','Metrics','','',@convertSpreadsheetDates);
R = ~cellfun(@isequalwithequalnans,dateNums,raw) & cellfun('isclass',raw,'char');
raw(R) = dateNums(R);
I've tried doing something similar after connecting to a database and running a SQL script but got an error (below). Now that the variable raw is cell array containing my results. Only one column is a date column (the rest are numeric or binary). The variable R does indeed output what I'd expect (1 for date, 0 for number). But the function call to datenum results in an error pasted below the code.
R = cellfun('isclass', raw, 'char');
raw(R) = datenum(R, 'yyyy-mm-dd');
data = cell2mat(raw);
Error using datenum (line 178) DATENUM failed.
Error in DatabaseImport (line 23) raw® = datenum(R, 'yyyy-mm-dd');
Caused by: Error using datenum (line 106) The input to DATENUM was not an array of strings.
Does anyone know an easy way to convert SQL dates to MATLAB dates?

채택된 답변

Sarah
Sarah 2014년 10월 29일
Import data in the form of a MATLAB dataset.
setdbprefs('DataReturnFormat', 'dataset');
Run query and assign data to output variable.
result = runsqlscript(connection, 'myQueryFile.sql');
out = result.Data;
Then use the column name that contains your dates as follows.
out.DateOfPeriod = datenum(out.DateOfPeriod, 'yyyy-mm-dd'); % convert from readable date to datenum
out.DateOfPeriod = datestr(out.DateOfPeriod); % convert back to readable date

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by