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

2 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2021-12-30
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.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2021-12-30
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Board games 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by