2013-03-06 124 views
1

我正在創建一個實用程序,它將爲用戶提示並詢問他們的crn#,部分#和教師姓。一旦進入,用戶將收到他們的考試時間,日期和地點。我遇到了我的Excel電子表格和使用mac os的問題。腳本工程完工雖然日期,只是表明了,而不是像(01/01/0001)在Matlab中使用Excel中的日期

這裏是代碼

這裏是電子表格,使代碼工作(); https://www.dropbox.com/s/drwmk1dinbfwfvl/DrexelFinalsWinter13.xlsx?m

close all; 

%% I created an Excel file to hold the information located on Drexel's 
%% website witch contains exam times and locations based on certain criteria 
%% First I have to make code to read the excel file created 
clc 
[ndata, text, alldata]=xlsread('DrexelFinalsWinter13.xlsx'); 
%% Next we will prompt the user for specific data by creating a dialog box 
%% using the prompt command. 
clc 
prompt={'Your CRN #','Your Section #',... 
    ' Your Instructors Name' }; 
numlines=1; 
defaultanswer={'22081','001','Hawkins'}; 
name='Enter Info'; 
options.Resize='on'; 
options.WindowStyle='normal'; 
options.Interpreter='tex'; 
answer=inputdlg(prompt,name,numlines,defaultanswer); %prompt user for data 

a1=cell2mat(answer(1));c1=str2double(a1); %converting cell info to numeric 
a2=cell2mat(answer(2));c2=str2double(a2); %converting cell info to numeric 
w=0; 



for p=1:1032; 
if (isequal(c1,cell2mat(alldata(p,1))) && ... 
     (or(isequal(c2,cell2mat(alldata(p,4))),... 
     isequal(answer(2),alldata(p,4)))) && ... 
     isequal(answer(3),alldata(p,6))); % the if condition looks to see if 
              % our input matches any data in the table 
    % we cant use cell data below for comoarison so I had to 
    % convert it to matlab common data like num or double 
    % for numbers because the xlsread function cant 
    % distinguish them and only know strings 
    w = w+1; 
    date = cell2mat(alldata(p,7)); 
    time = cell2mat(alldata(p,8)); 
    loca = cell2mat(alldata(p,9)); 
    fprintf('Your exam date is %s. \n',date); % print date results 
    fprintf('Your exam time is %s. \n',time); % print time results 
    fprintf('Your exam location is %s. \n',loca); % print location results 
    disp('Good luck with your exams! Sleep well & eat a healthy breakfast!'); % just for kicks 
elseif p==1032 && w==0 %if we didnt find any matches from our prompt 
    'You have provided wrong data or no exam is scheduled for your class.'; 
end 
end 
% The outputs are then located in the command line like below 
% answer to the defult values 
% Your exam date is 3/19/2013. 
% Your exam time is 0800-1000. 
% Your exam location is See Department. 
% Good luck with your exams! Sleep well & eat a healthy breakfast! 
+0

在'fprintf('考試日期是%s。\ n',日期)之前'date'的值是多少?'' ?在我剛剛執行命令date = cell2mat(alldata(p,7))後,請在變量窗口中查看調試器 – Dan 2013-03-06 14:53:15

+0

;它顯示它只是一個數字,如413566,而不是日期。這是Excel電子表格 – Chris 2013-03-06 14:58:03

+0

的一個問題所以你需要在'fprintf'顯示日期之前將該數字轉換爲日期字符串。 Excel也使用它自己的有趣的日期約定,所以我懷疑'datenum'等matlab將解決它,但我相信你可以在網上找到一個算法將Excel日期轉換爲字符串。 – Dan 2013-03-06 15:01:07

回答

2

試試這個:

fprintf('Your exam date is %s. \n',datestr(x2mdate(date))); % print date results 

看一看的x2mdate docs,如果你有足夠的財政工具箱否則,你可以這樣做:

fprintf('Your exam date is %s. \n',datestr(date + datenum('30DEC1899'))); 

(從here

+1

如果OP有財務工具箱,那很好。 – 2013-03-06 15:10:14

+0

@SamRoberts好點,我添加了一個替代 – Dan 2013-03-06 15:18:26

+0

不幸的是我沒有財務工具箱。第二種解決方案解決了我的問題,謝謝! – Chris 2013-03-06 18:17:23