Using a program designed function

조회 수: 1 (최근 30일)
James Keiser
James Keiser 2021년 6월 21일
답변: Steven Lord 2021년 6월 21일
Trying to call a function but it isnt working.
Code:
clear
clc
% Welcome message
fprintf('Welcome. This program uses the Zeller''s algorithm to compute\n')
fprintf('the day of the week for a given date. Outputs are given as\n')
fprintf('numbers between 0 - 6:\n')
fprintf('(0) - Sun \t(1)- Mon \t(2)- Tue \t(3)- Wed \t(4)- Thu \t(5)- Fri')
fprintf('\t(6)- Sat\n')
name = input('Please enter a name:','s');
fprintf('\nINPUT THE DOB:\n')
% Input section
Month = input('Month: ');
Day = input('Day: ');
Year = input('Year: ');
% run PDF
R = zelleralg('Month','Day','Year');
function:
function date = zelleralg(Month,Day,Year)
%function [date] = zelleralg(month,day,year)
% A = 1 plus the remainder of (the month number plus 9) divided by 12
A = 1+mod(Month+9,12);
% B = the day of the month
B = Day;
% C = the year of the century PLUS the ROUND DOWN from the equation: (0.09*Month-0.27).
C = (mod( Year , 1000 ))+floor(0.09*Month-0.27);
%D = the century
D = floor( Year / 100 );
% Additional integers necessary for the calculation
W = floor((13*A-1)/5);
X = floor(C/4);
Y = floor(D/4);
Z = W+X+Y+B+C-2*D;
% R is the day of the week, where 0 represents Sunday, 1 is Monday, . . ., 6 is Saturday
R = mod( Z, 7 );
  댓글 수: 2
Steven Lord
Steven Lord 2021년 6월 21일
What does "it isnt working" mean?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support using the telephone icon in the upper-right corner of this page so we can investigate.
Also, please don't start your code with "clear" and "clc". You don't need to run them every time you run your function, and having them as part of the normal execution of your code can interfere with the debugging process.
James Keiser
James Keiser 2021년 6월 21일
this is the error code :
Arrays have incompatible sizes for this operation.
Error in zelleralg (line 11)
C = (mod( Year , 1000 ))+floor(0.09*Month-0.27);
Error in Keiser_hw16 (line 29)
R = zelleralg('Month','Day','Year');

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

답변 (1개)

Steven Lord
Steven Lord 2021년 6월 21일
You don't want to pass the names of the variables into your function, you want to pass the variables themselves. As a simplified example, these two disp calls display very different answers.
x = 1:5;
disp('x') % name of the variable
x
disp(x) % contents of the variable
1 2 3 4 5
When you pass 'Month' and 'Year' into zelleralg, you're passing in a 1-by-5 char vector and a 1-by-4 char vector. What happens when you try to add a 1-by-5 vector and a 1-by-4 vector (leaving aside the question of adding characters)?
y = (1:5) + (1:4)
Arrays have incompatible sizes for this operation.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by