- 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.
Using a program designed function
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Steven Lord
il 21 Giu 2021
What does "it isnt working" mean?
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.
Risposte (1)
Steven Lord
il 21 Giu 2021
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
disp(x) % contents of the variable
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)
0 Commenti
Vedere anche
Categorie
Scopri di più su Install Products in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!