I have this function but whenever I call it using its name, I get an error

2 views (last 30 days)
I am building a game of battleship, and i have this function to create a figure for the boards.
now when ever i try to call it in a switch case using:
menu= menu(' play game',.....)
switch menu
case 1
setup
case 2
.
.
I keep get this error:
Error in Matlab>setup (line 93)
for ii = 1:(bs^2)
Error in Matlab (line 58)
setup
Can i please get help with how to fix it.
  1 Comment
John D'Errico
John D'Errico on 30 Dec 2021
Let me see if I understand. You are using the function named menu.
It returns a variable. You call that menu. Then you use menu in a switch case.
Should MATLAB know which version of menu should be used? The function? The variable? Even if you could make this work, do you understand how difficult your code will be to ever debug? An then, suppose you try to run this code twice in a row? MATLAB will now see a VARIABLE named menu. When you then try to use the FUNCTION named menu, MATLAB will get really confused, and throw an error.
What are the odds your code will run correctly? I certainly won't be taking any bets on the matter.

Sign in to comment.

Accepted Answer

Voss
Voss on 30 Dec 2021
setup is a function that takes one input argument, bs, so when you call it you need to call it like:
setup(10); % to set up a board size of 10
or, equivalently:
my_board_size = 10;
setup(my_board_size);

More Answers (0)

Categories

Find more on Board games 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!