在Matlab中使用Excel中的日期

时间:2013-03-06 14:45:08

标签: excel matlab

我正在创建一个实用程序,它会向用户提示并询问他们的crn#,section#和教师姓氏。一旦输入,用户将收到他们的考试时间,日期和地点。我的excel电子表格和使用mac os有问题。虽然日期显示为(。)而不是像(01/01/0001)

,但脚本可以完成。

这是代码

这是使代码有效的电子表格; 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!

1 个答案:

答案 0 :(得分: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