Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
function- help needed!

Subject: function- help needed!

From: lou lou Pengant

Date: 17 Aug, 2009 11:27:02

Message: 1 of 3

Create a matlab function called birthday. The argument of the function is a , where a is a persons age.

if a is less than 21 then the function must output a text string of the following form:
eg, if a=7, the text string should be 'I am 7 years old'
     if a=10, the text string should be 'I am 10 years old'

However, if a is 21 or over, the output text string should be 'I have passed my 21st birthday'


I am having difficulty with creating the first part of the question, when ages are less than 21- I dont know how to make the age in the text string a variable.

function [display]=birthday(a)
a=input('How old are you?');
if a>=21;
    disp('I have passed my 21st birthday')
elseif a<21
THIS IS THE LINE THAT I NEED HELP ON!!
end
end

Subject: function- help needed!

From: us

Date: 17 Aug, 2009 11:33:01

Message: 2 of 3

"lou lou Pengant" <bad22@bad.com> wrote in message <h6beq6$1e1$1@fred.mathworks.com>...
> Create a matlab function called birthday. The argument of the function is a , where a is a persons age.
>
> if a is less than 21 then the function must output a text string of the following form:
> eg, if a=7, the text string should be 'I am 7 years old'
> if a=10, the text string should be 'I am 10 years old'
>
> However, if a is 21 or over, the output text string should be 'I have passed my 21st birthday'
>
>
> I am having difficulty with creating the first part of the question, when ages are less than 21- I dont know how to make the age in the text string a variable.
>
> function [display]=birthday(a)
> a=input('How old are you?');
> if a>=21;
> disp('I have passed my 21st birthday')
> elseif a<21
> THIS IS THE LINE THAT I NEED HELP ON!!
> end
> end

a hint:

     help sprintf;

us

Subject: function- help needed!

From: Steven Lord

Date: 17 Aug, 2009 13:21:40

Message: 3 of 3


"lou lou Pengant" <bad22@bad.com> wrote in message
news:h6beq6$1e1$1@fred.mathworks.com...
> Create a matlab function called birthday. The argument of the function is
> a , where a is a persons age.
>
> if a is less than 21 then the function must output a text string of the
> following form:
> eg, if a=7, the text string should be 'I am 7 years old'
> if a=10, the text string should be 'I am 10 years old'
>
> However, if a is 21 or over, the output text string should be 'I have
> passed my 21st birthday'
>
>
> I am having difficulty with creating the first part of the question, when
> ages are less than 21- I dont know how to make the age in the text string
> a variable.
>
> function [display]=birthday(a)
> a=input('How old are you?');
> if a>=21;
> disp('I have passed my 21st birthday')
> elseif a<21
> THIS IS THE LINE THAT I NEED HELP ON!!
> end
> end

Note that your code receives an input argument, a, when you call it ... but
then you overwrite that input argument with the output argument from your
INPUT call. I'm guessing this is a homework assignment (and the rest of
this post will follow that assumption) and I'm also guessing that your
professor will call that behavior out when he or she grades it. I would
remove that INPUT statement entirely.

Also, you specify in the function declaration line that this function will
return one output argument, the variable inside birthday that's called
display. However, you never create a variable named display inside
birthday, so you will probably receive an error when you call this function
with an output argument. Because of that and the wording of the problem
statement you posted above, this program does not satisfy that problem
statement, and your professor will likely call that out too during grading.
Rather than displaying the string inside birthday, return it as an output
argument.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us