I have this function but whenever I call it using its name, I get an error
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tariq Hammoudeh
am 30 Dez. 2021
Bearbeitet: Tariq Hammoudeh
am 1 Jan. 2022
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 Kommentar
John D'Errico
am 30 Dez. 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.
Akzeptierte Antwort
Voss
am 30 Dez. 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);
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!