Dates not being handled correctly in script
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Database Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!