Clear Filters
Clear Filters

Dates not being handled correctly in script

2 views (last 30 days)
Hi there,
I'm trying to write a function to import data from a Microsoft access database between two dates. I'm using the database explorer toolbox. I used the GUI to generated code to do the task and then I modified it so that I could pass in 2 dates as arguments to the function.
However, for some reason the dates are not being handled/converted properly so the script returns no data.
It would be great if somebody could look at the code and tell me where I'm going wrong.
In the command line I'm writing
data = getData('14/7/2009','20/7/2009');
Using the debugging it says the startdate = 30/12/0019 and enddate = 30/12/0025 which are obviously incorrect.
Many thanks
Code:
function data = getData(startDate, endDate)
%This script import data from the ElectricityData database access file
%Set preferences with setdbprefs.
setdbprefs('DataReturnFormat', 'structure');
setdbprefs('NullNumberRead', 'NaN');
setdbprefs('NullStringRead', 'null');
%Make connection to database. Note that the password has been omitted.
%Using ODBC driver.
conn = database('electricitydata', '', '');
%Read data from database.
if nargin == 2
startDate = datestr(startDate, 'dd/mm/yyyy');
endDate = datestr(endDate, 'dd/mm/yyyy');
curs = exec(conn, ['SELECT LoadData.Date'...
' , LoadData.Hour'...
' , LoadData.Temperature'...
' , LoadData.Load'...
' FROM LoadData WHERE LoadData.Date BETWEEN #' startDate '# AND #' endDate '# ']);
else
curs = exec(conn, ['SELECT LoadData.Date'...
' , LoadData.Hour'...
' , LoadData.Temperature'...
' , LoadData.Load'...
' FROM LoadData ']);
end
curs = fetch(curs);
close(curs);
%Assign data to output variable
data = curs.Data;
%Close database connection.
close(conn);
%Clear variables
clear curs conn

Accepted Answer

Vishal Rane
Vishal Rane on 11 Oct 2013
datestr converts date vectors to date strings. since you already have a date string I believe you need to use datenum

More Answers (0)

Community Treasure Hunt

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

Start Hunting!