Output shows an 'ans=', I don't know where did i go wrong

1 view (last 30 days)
So i have a program about calculating the current age of the user, my code is much similar to https://www.mathworks.com/matlabcentral/fileexchange/43764-age_calculator, i only copied the process of calculation, and no gui.
So everytime i run my code, it keeps showing the "ans=" at the end and i dont know where i did wrong, can u help me out?
here is my code
function [day,month,year,YY,MM,DD,message]=bday_calc(~,~,~,~,~)
%The program will ask question to the user about his Birth date,month and
%year
disp ('What day did you born? ');
day= (' ');
BdayDate= input (day);
%if the user typed value above 31, the program will give an error
if BdayDate > 31
disp('This is not a valid answer, Run the program again!');
return
end
if isempty(BdayDate)
disp('This is not a valid answer, Run the program again!');
return
end
disp ('What month did you born?');
disp ('In numerical value e.g (1=January) ');
month= ('');
BdayMonth= input (month);
%if the user typed value above 12, the program will give an error
while BdayMonth > 12
disp('This is not a valid answer, Run the program again!');
return
end
%For the Year, using the clock command allows to get the current day and converts it
%into vectors.
Time = clock;
DateToday = round([Time(3) Time(2) Time(1)]);
disp ('What year did you born');
year= ('');
Year= input (year);
%When the user input a year ahead from the current year, the program will
%give an error
if Year > DateToday (3)
disp('This is not a valid answer, Run the program again!');
return
end
if Year < 1000
disp('Please input valid year');
return
end
%This part calculates the age of the user
DateOfBirth=[BdayDate,BdayMonth,Year];
YY = DateToday(3) - DateOfBirth(3);
MM = DateToday(2) - DateOfBirth(2);
DD = DateToday(1) - DateOfBirth(1);
if DD < 0
DD = DD+30;
MM = MM-1;
end
if YY < 100
YY=YY+2000;
end
if MM < 0
MM = MM+12;
YY =YY-1;
end
if MM<12
end
Age = [YY,MM,DD];
%Pop-up message
message = ['Your Age Is ' num2str(Age(1)) ' Year(s) ' num2str(Age(2)) ' Month(s) ' num2str(Age(3)) ' day(s) '];
disp (message)
end
this is the result

Accepted Answer

Alberto Mora
Alberto Mora on 12 Feb 2021
Edited: Alberto Mora on 12 Feb 2021
Suppress output the function when you call it, like follow:
bday_calc; % add the ; symbol
  2 Comments
Jan Kyle Dismas
Jan Kyle Dismas on 12 Feb 2021
Oh im very sorry about the title, im very new and i dont know everything yet. but thank you for your answer, i just put the bday_calc; at the end of my program and it works well

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 12 Feb 2021
Your function is defined to return up to seven output arguments.
function [day,month,year,YY,MM,DD,message]=bday_calc(~,~,~,~,~)
When you call it with zero output arguments and ask MATLAB to display the output (by not ending the line with a semicolon) MATLAB will by default assign the value of the first output to the variable named ans (for ANSwer) and then display ans.
Alberto Mora's answer suppresses the display by ending the call with a semicolon. You could also, if you want to work with some or all of those outputs later in your session, call bday_calc with between one and seven output arguments.
[thisIsTheDay, thisIsTheMonth] = bday_calc
The year, YY, MM, DD, and message outputs will be ignored in this call since you didn't ask for them.

Community Treasure Hunt

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

Start Hunting!