필터 지우기
필터 지우기

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