Problem with an if statement

1 view (last 30 days)
Tariq Hammoudeh
Tariq Hammoudeh on 30 Dec 2021
Commented: Voss on 2 Jan 2022
I have this code:
orientation= input (' enter v or h')
if orientation == v
......
end
but whenever i run the code and enter v or h, I get an error saying
Unrecognized function or variable 'v'.
So does matlab not take inputs in letters or is there a way to make it work.

Accepted Answer

Voss
Voss on 30 Dec 2021
Use the optional second argument to the input() function, 's', which tells input() to return exactly what the user entered, without evaluating it. You'll also need to compare orientation to 'v' rather than v or you'll just get the same error on the next line (and use strcmp/strcmpi in case they entered more than one character or an empty string).
orientation= input (' enter v or h', 's')
if strcmp(orientation,'v')
% do v stuff
else
% do h stuff
end
  11 Comments
Tariq Hammoudeh
Tariq Hammoudeh on 2 Jan 2022
Edited: Tariq Hammoudeh on 2 Jan 2022
@Benjamin Again thank you i added this to my other code and it works fine, but i just want to ask what does the for loop do, I understood everything else but i just want to know what does this for loop do.
Voss
Voss on 2 Jan 2022
The for loop is to loop over the different ships, asking the user for each ship's startingGrid and orientation and placing each ship on the board.

Sign in to comment.

More Answers (0)

Categories

Find more on Conway's Game of Life in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!