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:
User Entry if and statement

Subject: User Entry if and statement

From: Ender

Date: 18 Apr, 2008 00:26:02

Message: 1 of 4

I am writing code that is going to ask a question than do
certain routines after the user inputs an answer for the
first question.

user_entry = input('i or e? ','s'); % ask for user
input asking which function do you want to solve for


if 'i';
    disp('cool');
elseif 'e';
    disp('ok');
else disp('Please try again')
    
end

The display was just for testin purposes, but no matter
what I type in for the response to the user_entry
statement. MATLAB always displays 'cool'. MATLAB says that
I have a character string variable for whatever letter I
respond to the first question with, but then MATLAB always
responds with 'cool' instead of performing the function of
the if and statement.

-Ender-

Subject: User Entry if and statement

From: ImageAnalyst

Date: 18 Apr, 2008 02:53:45

Message: 2 of 4

On Apr 17, 8:26=A0pm, "Ender " <jr...@msstate.edu> wrote:
> I am writing code that is going to ask a question than do
> certain routines after the user inputs an answer for the
> first question.
>
> user_entry =3D input('i or e? =A0 =A0 ','s'); =A0 % ask for user
> input asking which function do you want to solve for
>
> if 'i';
> =A0 =A0 disp('cool');
> elseif 'e';
> =A0 =A0 disp('ok');
> else disp('Please try again')
>
> end
>
> The display was just for testin purposes, but no matter
> what I type in for the response to the user_entry
> statement. MATLAB always displays 'cool'. MATLAB says that
> I have a character string variable for whatever letter I
> respond to the first question with, but then MATLAB always
> responds with 'cool' instead of performing the function of
> the if and statement.
>
> -Ender-

Ender:
Sure, because 'i' is non-zero, when converted to a logical. Try using
the strcmpi() function to compare your results to 'i' or 'e' strings.
Regards,
ImageAnalyst

Subject: User Entry if and statement

From: Ender

Date: 18 Apr, 2008 19:28:02

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in
message <5e1093e8-63bd-41a1-9d9b-
a40fbfd3990e@a23g2000hsc.googlegroups.com>...
> On Apr 17, 8:26=A0pm, "Ender " <jr...@msstate.edu> wrote:
> > I am writing code that is going to ask a question than
do
> > certain routines after the user inputs an answer for
the
> > first question.
> >
> > user_entry =3D input('i or e? =A0 =A0 ','s'); =A0 %
ask for user
> > input asking which function do you want to solve for
> >
> > if 'i';
> > =A0 =A0 disp('cool');
> > elseif 'e';
> > =A0 =A0 disp('ok');
> > else disp('Please try again')
> >
> > end
> >
> > The display was just for testin purposes, but no matter
> > what I type in for the response to the user_entry
> > statement. MATLAB always displays 'cool'. MATLAB says
that
> > I have a character string variable for whatever letter
I
> > respond to the first question with, but then MATLAB
always
> > responds with 'cool' instead of performing the
function of
> > the if and statement.
> >
> > -Ender-
>
> Ender:
> Sure, because 'i' is non-zero, when converted to a
logical. Try using
> the strcmpi() function to compare your results to 'i'
or 'e' strings.
> Regards,
> ImageAnalyst


I tried what you said and it worked. My new problem is
that I want my MatLab code to take one of two paths after
they pick 'i or e'. If they pick 'i' I want my code to ask
a series of questions and get input from the user to put
into variables. If they pick 'e' I want MATLAB to go
through a different routine and ask a list of questions
and have the input stored as variables.

The problem is that MATLAB says:

'The expression to the left of the equals sign is not a
valid target for an assignment.'

MATLAB is speaking of w=1 in the second if and statement.
It seems to me that MATLAB is saying that I the variable
cannot exist because I haven't defined it even though the
beginning of the code which would write the value for the
variable hasn't been written yet. Here is my code, it
might explain it better than I can.

disp('Do you want to find the sun-synchronous orbit as a
function of')
disp('inclination angle or eccentricity?')


user_entry = input('i or e? ','s'); % ask for user
input asking which function do you want to solve for


if strcmpi(user_entry,'i');
    w=1;
elseif strcmpi(user_entry,'e');
    j=1;
elseif disp('That is an incorrect choice! Please try
again!')

endseif

if w=1;
    user_entry=('What is the semi-major axis (km)?')
else w=0
end
  
Do you know how I can fix this?

Subject: User Entry if and statement

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 18 Apr, 2008 19:51:33

Message: 4 of 4

In article <fuaso1$nj7$1@fred.mathworks.com>, Ender <jr147@msstate.edu> wrote:
>if w=1;
> user_entry=('What is the semi-major axis (km)?')
>else w=0
>end

>Do you know how I can fix this?

if w==1
  user_entry=input('What is the semi-major axis (km)?');
else
  w=0
end

--
   So you found your solution
   What will be your last contribution?
   -- Supertramp (Fool's Overture)

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