Convert SQL dates to MATLAB datenums

6 次查看(过去 30 天)
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