You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Problem with an if statement
1 view (last 30 days)
Show older comments
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
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
on 30 Dec 2021
Edited: Tariq Hammoudeh
on 30 Dec 2021
Thank you, for that but i just ended up using
if orientation == 'v'
....
But i have another question related to this,
Im using
if orientation == 'v'
....
elseif orientation == 'h'
.....
else
disp('choose v or h only')
end
How can i make it so that if neither v nor h are entered it displays this message and allows the user to try again
Voss
on 30 Dec 2021
orientation = '';
while ~ismember(orientation,{'v' 'h'})
orientation = input(' enter v or h', 's');
if strcmp(orientation,'v')
% do v stuff
elseif strcmp(orientation,'h')
% do h stuff
else
disp('choose v or h only');
end
end
Or:
while true
orientation = input(' enter v or h', 's');
if ismember(orientation,{'v' 'h'})
break;
end
disp('choose v or h only');
end
if strcmp(orientation,'v')
% do v stuff
else
% do h stuff
end
Tariq Hammoudeh
on 30 Dec 2021
ok just one more thing, I joined this with my code and now its like this:
startingGrid= input(' Please choose the grid number you want your ship to start in : ');
if startingGrid == 1
while true
orientation = input(' Please enter v if you want your ship to be placed vertically or h if you want it horizontally', 's');
if ismember(orientation,{'v' 'h'})
break;
end
disp(' please choose v or h only and make sure to use small letters ');
end
if strcmp(orientation,'v')
.....
else
.....
end
I have many of those if statements, and i need to be able to let the user enter another startingGrid if they chose a specific number then either v or h. So basically in the
% do v stuff
or
% do h stuff
i tried
startingGrid=input('please try again')
but whenever i enter a different number the program just ends.
please note those "specific numbers" are things that i have different code to detect, so i only need the program to continue when i "try again", this is the only issue
Voss
on 30 Dec 2021
I would make the startingGrid input and the orientation input independent of each other. Something along the lines of this:
while true
startingGrid = input(' Please choose the grid number you want your ship to start in : ');
% validate startingGrid with your other code here
if startingGrid >= 1 && startingGrid <= 10 % whatever your valid condition is
break
end
disp('please try again');
end
while true
orientation = input(' Please enter v if you want your ship to be placed vertically or h if you want it horizontally', 's');
if ismember(orientation,{'v' 'h'})
break
end
disp(' please choose v or h only and make sure to use small letters ');
end
then validate startingGrid and orientation together after that, i.e., check some condition(s) that depend(s) on both startingGrid and orientation, which is not possible to check until both have been input.
This way you only ever need one while loop for each input, rather than many.
You probably will want to wrap that whole thing in a while loop too, for the consistency check between startingGrid and orientation:
while true
while true
startingGrid = input(' Please choose the grid number you want your ship to start in : ');
% validate startingGrid with your other code here
if startingGrid >= 1 && startingGrid <= 10 % whatever your valid condition is
break
end
disp('please try again');
end
while true
orientation = input(' Please enter v if you want your ship to be placed vertically or h if you want it horizontally', 's');
if ismember(orientation,{'v' 'h'})
break
end
disp(' please choose v or h only and make sure to use small letters ');
end
% check consistency between startingGrid and orientation
% if valid, break
% else, issue statment (and the big while loop continues)
end
Tariq Hammoudeh
on 30 Dec 2021
Im sorry i realized i wasnt too clear, those 'specific numbers" are just grid numbers where if you choose v or h on them the ship will be placed out of bounds, so what my actual code is:
disp(' You will now place your corvette (1X2) ship')
startingGrid= input(' Please choose the grid number you want your ship to start in : ');
% orientation= input(' Please enter v if you want your ship to be placed vertically or h if you want it horizontally', 's');
if startingGrid == 1
while true
orientation = input(' Please enter v if you want your ship to be placed vertically or h if you want it horizontally', 's');
if ismember(orientation,{'v' 'h'})
break;
end
disp(' please choose v or h only and make sure to use small letters ');
end
if strcmp(orientation,'v')
...
else
...
end
elseif startingGrid == 2
.. repeat the same code above but do different v and h stuff..
elseif startingGrid == 3
.....
So what i need for example is when startingGrid == 6
if startingGrid == 6
while true
orientation = input(' Please enter v if you want your ship to be placed vertically or h if you want it horizontally', 's');
if ismember(orientation,{'v' 'h'})
break;
end
disp(' please choose v or h only and make sure to use small letters ');
end
if strcmp(orientation,'v')
..
else
display ('ship will be out of bounds please try again') and let the user try again
end.
because here when its h and starting grid 6 the ship will be out of bounds
Note: Im very well aware this isnt the most convenient way as ill have a over a 100 if statements, and ill have to go through each one individually, but its the only way i can think of.
Voss
on 31 Dec 2021
If you can think of a way to combine orientation, startingGrid, the size of the grid, and the size of the ship into one or a few conditions to check, you'll be much better off and have many fewer if statements. For example:
grid_size = 6;
ship_size = 2;
startingGrid = 5;
orientation = 'h';
if startingGrid+ship_size-1 > grid_size
disp('that''s a problem');
else
disp('that''s ok');
end
that's ok
grid_size = 6;
ship_size = 2;
startingGrid = 6;
orientation = 'h';
if startingGrid+ship_size-1 > grid_size
disp('that''s a problem');
else
disp('that''s ok');
end
that's a problem
grid_size = 6;
ship_size = 3;
startingGrid = 5;
orientation = 'h';
if startingGrid+ship_size-1 > grid_size
disp('that''s a problem');
else
disp('that''s ok');
end
that's a problem
Tariq Hammoudeh
on 2 Jan 2022
Edited: Tariq Hammoudeh
on 2 Jan 2022
@Benjamin Thank you but, if the user enters 7 and h, then the above code wont let him, even though it should, so is there a way to fix it.
I tried:
while orientation=='h' && startingGrid==6 || orientation=='h' && startingGrid==12 % and so on for all conditions that will result in out of bounds
disp('Your ship will be placed out of bounds ')
startingGrid=input('Enter a grid number you want your ship to start in again : ');
orientation=input('Please enter v if you want your ship to be placed vertically or h if you want it horizontally : ','s');
end
and it works, but then for the bigger ships this line would get tooo long, so is there a way to fix the previous code so i can use something like
while orientation =='h' && startingGrid+shipSize-1 > bs
...
end
instead of the very long line
Voss
on 2 Jan 2022
I would do it something like this. It doesn't check that ships don't overlap with each other, but it does check that each ship is on the board completely. I believe it has the structure you are looking for.
This code uses a 6-by-6 board, and assumes the definition of startingGrid is as follows: startingGrid = 1, 2, 3, 4, 5, 6 corresponds to the top row of the board, 7 is second row, first column, etc. (which is implied by your code). (Because this indexing is the opposite of how MATLAB does linear indexing in 2d matrices, it is convenient to build the transpose of the user-specified board and then transpose it at the end to get the final result.)
grid_size = 6;
board = zeros(grid_size);
ship_sizes = [2 3 5];
for i = 1:numel(ship_sizes)
while true
while true
startingGrid = input(' Please choose the grid number you want your ship to start in : ');
% validate startingGrid with your other code here
if startingGrid >= 1 && startingGrid <= grid_size^2
break
end
disp('please try again');
end
while true
orientation = input(' Please enter v if you want your ship to be placed vertically or h if you want it horizontally', 's');
if ismember(orientation,{'v' 'h'})
break
end
disp(' please choose v or h only and make sure to use small letters ');
end
% check consistency between startingGrid and orientation
n_max = grid_size-ship_sizes(i)+1;
if strcmp(orientation,'h')
if mod(startingGrid-1,grid_size)+1 > n_max
disp('Your ship will be placed out of bounds (off the right side of the board)');
continue
end
% if valid, place the ship ...
board(startingGrid+(0:ship_sizes(i)-1)) = i;
else
if floor((startingGrid-1)/grid_size)+1 > n_max
disp('Your ship will be placed out of bounds (off the bottom of the board)');
continue
end
% if valid, place the ship ...
board(startingGrid+grid_size*(0:ship_sizes(i)-1)) = i;
end
% ... and break
break
end
end
board = board.';
disp(board);
Tariq Hammoudeh
on 2 Jan 2022
Thank you
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.
More Answers (0)
See Also
Categories
Find more on Conway's Game of Life in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)