Write a function that displays the next day when you input the previous day in vector form i.e. [mm dd yyyy]

I just started control structures (for loops and if statements) in Mathlab, and I have to write a function that takes in a vector of three numbers in the form [mm dd yyyy] and outputs the next day in the same form using loops and if statements.
Here is what I'm thinking so far:
function [A B C] = next_day[X Y Z] % X=mm Y=dd Z=yyyy
if X== 01 || 03 || 05 || 07 || 08 || 10 || 12
Y==1:31
if X== 04 || 06 || 09 || 11
Y==1:30
if X== 02 & rem(Z,100)>0 %Not a leap year February has only 28 days
Y==1:28
if X== 02 & rem(Z,4)==0 || rem(Z,400)==0 %Leap year February has a 29th day
Y==1:29
I know that is far from being correct, but I do not know how to implement loops to output the next day. Any comments would help. Thanks in advance!

2 件のコメント

dpb
dpb 2015 年 10 月 11 日
I don't see any need for any looping here; simply add a day to the input day and then check for wrapping of the month and year fields.
Rock2019
Rock2019 2015 年 10 月 11 日
編集済み: Rock2019 2015 年 10 月 11 日
How would you do that if you don't mind me asking?
I need to be able to input [12 31 2000] which should output [01 01 2001]
and
[02 28 1900] which should output [03 01 1800]
and [02 28 1600] which should output [02 29 1600]
and various other dates.
Thanks!

サインインしてコメントする。

回答 (2 件)

if X== 01 || 03 || 05 || 07 || 08 || 10 || 12
something#1
end
means the same as
if X == 01
something#1
elseif 03
something#1
elseif 05
something#1
...
end
That is, the "X ==" part applies *only* to the immediate next value, 01, and after that you are testing the truth or falseness of the numbers 03, 05, and so on.
If you want to test to see if X is one of those values you need to include the "X==" in each of the tests:
if X== 01 || X== 03 || X== 05 %etc
You could also consider using ismember()
dpb
dpb 2015 年 10 月 12 日
A beginning...first use some variables that make some mnemonic sense...
function [m d y] = next_day(m, d, y)
d=d+1; % add a day
if d>eomday(y,m)
...
OK, now what?
If you're not allowed to use handy functions such as eomday then store the days/per month in an internal lookup array or similar workaround...it's relatively easy to code an isleapyr function from first principles of not ending in 4 excepting for 100 excepting 400 rule. Again, these depend upon what ground rules have been set of what you can use or what you can think of clever ways of using builtin functions. I don't know if TMW has added one specifically since R2012b but it's a one-liner there with what's already been shown here but if that's not allowed, you'll have to brute-force it.
NB: In my function definition I accepted the three values as separate numeric variables; if you are required to use a vector then you'll have to separate out those elements depending on how you define the input. The three numeric values would be in keeping with TMW-supplied functions such as datenum and friends, however.

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2015 年 10 月 11 日

回答済み:

dpb
2015 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by