function [x] = holiday( month, day )
M = [1,2,3,4,5,6,7,8,9,10,11,12];
D = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
if nargin < 1
error('The input must be a positive integer');
end
if ~isscalar(month) || month <1 || month ~= fix(month)
error ('The input "Month" must be positive integer');
elseif ~isscalar(day) || day <1 || day ~= fix(day)
error ('The input "Day" must be positive integer');
end
*if(M == 1 && D ==1) || (M == 7 && D == 4) || (M == 12) && D == 25) || (M == 12 && D == 31)*
x = true;
else
x = false;
end
end
Matlab says that The text that i have made bold is wrong. Error occurs:,saying: Unexpected bracket or parenthesis.
Can anyone please clarify this for me?

댓글 수: 5

KSSV
KSSV 2018년 8월 8일
What is that you are trying? Your code is a mess.
Yash Sunil Runwal
Yash Sunil Runwal 2018년 8월 8일
Write a function called holiday that takes two input arguments called month and day; both are scalar integers representing a month (1-12) and a day (1-31). You do not need to check that the input is valid. The function returns a logical true if the specified date is a holiday; if not, it returns false. For the purposes of this exercise, the following dates are considered holidays: January 1st, July 4th, December 25th, and December 31st.
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 8일
Which line the error it shows?
Yash Sunil Runwal
Yash Sunil Runwal 2018년 8월 8일
Line 12. The IF Statement
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 8일
I have edited the answer, pls check and confirm. I have removed all non-mandatory lines, you can add them after successfully execute the code.

댓글을 달려면 로그인하십시오.

 채택된 답변

Dennis
Dennis 2018년 8월 8일

0 개 추천

As Kalyan Acharjya has already shown the actual error is a wrong parenthesis.
if(M == 1 && D ==1) || (M == 7 && D == 4) || (M == 12) && D == 25) || (M == 12 && D == 31)
^^^^ ^^^^
However your code will still not work as intented. You create 2 vectors M and D, that you do not need for the given task (but you should try D=1:31 in command line).
For the actual task your code ignores the function inputs (month,day) and uses your created vectors. A corrected version might look like this:
function x=holiday(M,D) %i was too lazy to change all the Ms and Ds to month and day....
if(M == 1 && D ==1) || (M == 7 && D == 4) || (M == 12 && D == 25) || (M == 12 && D == 31)
x=true;
else
x=false;
end
end
If you want to check for correct input aswell, please consider that
if nargin < 1 %should be if nargin ~=2, however your function will throw an error anyway if called with less or more than 2 inputs and this line does not get executed....
error('The input must be a positive integer'); %you are checking the number of inputs, not if the input is an integer or string or vector or whatever
end
if ~isscalar(month) || month <1 || month ~= fix(month) %maybe check for >12/31 aswell, not sure about the rounding
error ('The input "Month" must be positive integer');
elseif ~isscalar(day) || day <1 || day ~= fix(day)
error ('The input "Day" must be positive integer');
end

댓글 수: 1

Yash Sunil Runwal
Yash Sunil Runwal 2018년 8월 8일
Thanks. I was over complicating it for no reason. It worked.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 8일
편집: KALYAN ACHARJYA 2018년 8월 8일

0 개 추천

function x=holiday(m,d)
i={1,2,3,4,5,6,7,8,9,10,11,12};
j={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
if(i{m}==1 && j{d}==1) || (i{m}==7 && j{d}==4) || (i{m}==12 && j{d}==25) || (i{m}==12 && j{d}==31)
x='true';
else
x='false';
end
end

댓글 수: 6

KSSV
KSSV 2018년 8월 8일
Note that, in the function M, D are vectors......equating a vector to scalar wont be of use.
Yash Sunil Runwal
Yash Sunil Runwal 2018년 8월 8일
Wait, I didn't understand. I typed the line you mentioned and it's still not showing correct answer
Yash Sunil Runwal
Yash Sunil Runwal 2018년 8월 8일
Yes. After that I used single '&' and single '|', as they are vectors. But it still didn't help.
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 8일
Thank you @KSSV Sir
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 8일
@ Yash Sunil I have tested the code in small way, you can do the same
for more data.
function [x] = holiday( month, day )
if nargin < 1
error('The input must be a positive integer');
end
if ~isscalar(month) || month <1 || month ~= fix(month)
error ('The input "Month" must be positive integer');
elseif ~isscalar(day) || day <1 || day ~= fix(day)
error ('The input "Day" must be positive integer');
end
if (month == 1 && day ==1) || (month == 7 && day == 4) || (month == 12 && day == 25) || (month == 12 && day == 31)
x = true;
else
x = false;
end
end
if true
% code
end
This also worked.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Calendar에 대해 자세히 알아보기

태그

질문:

2018년 8월 8일

댓글:

2018년 8월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by