计算一年中第n天的代码

时间:2016-04-07 21:20:07

标签: matlab

我想编写一个matlab代码,以获取哪一天是一年的第一天,并获得n并显示哪一天是该年的第n天。 而且我也不知道为什么它不能比较(例如sat)和w(i) (W = [星期六,星期日,...,周五] 请帮助我,我真的不能让它发挥作用!

这是我到目前为止所做的:

First=input('sat,sun,...,fri');
day=('a number between 1and 365');
day=mod(day,7);
w=[sat,sun,....,fri];
for i=1:7
    if first==w(i)
        disp(mod(i+day,7))
    end
end

1 个答案:

答案 0 :(得分:2)

请注意以下代码中的语法。我注意到你的语法中有很多错误。另外,我建议您使用datenumdatestr,如下面的代码所示。运行help datenumhelp datestr以获取有关这些功能的更多信息。

% User selects a year as a double
year = input('Select a year: '); 

% the first day of that year as a value
date = datenum([num2str(year),'-01-01']); 

% Get the name of the first day and diplay it
first = datestr(date,'dddd'); 
disp(['The first day of ', num2str(year), ' was a ', first])

% get nth day from user
day = input('Choose a number between 1 and 365: '); 

% Add this value to the value of 1st jan on the selected year
newDate = date + day-1; 

% Turn this date into a string and display it
nth = datestr(newDate,'dddd-dd-mmmm'); 
disp(['Day ', num2str(day),' of ', num2str(year), ' was ', nth])