From: Sarah on
The assignment is to make a script that will let the user input a month such as 'January' or 'Jan' and have the output be something like 'The month is 1.' (For Feb, 'The month is 2.' and so on...)

The instructions says to use if...elseif and it also told me to use the help function to find a way to compare strings.

Here is what I've gotten so far:

Please let me know what I'm doing wrong and nudge me in the right direction! I'm not asking anyone to do my homework for me, just a bit of help! Thanks.

%%%%%%%%%%%%%%%%%%%%%%
input('Enter Month Name: ');
if January =='Month';
strcmpi(January,'Month');
disp('The Month is 1')
elseif Jan=='Month';
strcmpi(Jan,'Month');
disp('The Month is 1')
end;
%%%%%%%%%%%%%%%%%%%%%%
^everytime I run this script, I get an error message saying 'January' is undefined.

The script keeps going on to include all of the months, but it was the same work so i shortened it to keep it rather simple!
From: Sarah Nguyen on
I also want to add that I have found a way to do make this script by defining all of the months individually,

EX:
%%%%%%%%%%%%%
January=1;
Jan=1;
February=2;
Feb=2;
March=3
%...
%and so on and then continue on with a simple script.

But my professors requires us to use if...elseif and script compare!
From: Eric Diaz on
Hi Sarah,

It doesn't make sense to have two logic statements that are comparing the same thing written back to back. If statements and comparison statements are both logical.

Perhaps that is where you are getting stuck. A little nudge is all I can offer.

Eric
From: Ilham Hardy on
"Sarah " <properplz(a)yahoo.com> wrote in message <hncm1e$c04$1(a)fred.mathworks.com>...
> The assignment is to make a script that will let the user input a month such as 'January' or 'Jan' and have the output be something like 'The month is 1.' (For Feb, 'The month is 2.' and so on...)
>
> The instructions says to use if...elseif and it also told me to use the help function to find a way to compare strings.
>
> Here is what I've gotten so far:
>
> Please let me know what I'm doing wrong and nudge me in the right direction! I'm not asking anyone to do my homework for me, just a bit of help! Thanks.
>
> %%%%%%%%%%%%%%%%%%%%%%
> input('Enter Month Name: ');
> if January =='Month';
> strcmpi(January,'Month');
> disp('The Month is 1')
> elseif Jan=='Month';
> strcmpi(Jan,'Month');
> disp('The Month is 1')
> end;
> %%%%%%%%%%%%%%%%%%%%%%
> ^everytime I run this script, I get an error message saying 'January' is undefined.
>
> The script keeps going on to include all of the months, but it was the same work so i shortened it to keep it rather simple!

Hi,

CMIIW,
Hint:
> if January =='Month';
> strcmpi(January,'Month');

should be..
if strcmpi(January,'Month');
From: Jan Simon on
Dear Sarah!

> input('Enter Month Name: ');

Although the user can write the name of the month, it is not stored anywhere. Look at the help of the function "input":
help input
There you find an example of how to store a typed string (!) in the replied variable R.

> if January =='Month';
> strcmpi(January,'Month');
> disp('The Month is 1')

" January == 'Month' " is confusing. The variable "January" was not defined before and you compare it elementwise (see: help eq) with the string 'Month'. 'Month' consists of 5 CHARs, to usually the test will fail.
But in the next line, the comparison is almost ok, just swap the string and variable:
if strcmpi(Month, 'January');
If you've store the user input in the variable Month, the first month is recognized correctly!

Welcome to Matlab and good luck, Jan