Filter löschen
Filter löschen

Convert SQL dates to MATLAB datenums

3 Ansichten (letzte 30 Tage)
Sarah
Sarah am 27 Okt. 2014
Beantwortet: Sarah am 29 Okt. 2014
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?

Akzeptierte Antwort

Sarah
Sarah am 29 Okt. 2014
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

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by