Local functions are not working in MatLab R2022a

조회 수: 4(최근 30일)
Jonne Klockars
Jonne Klockars 2022년 6월 9일
댓글: Jonne Klockars 2022년 6월 9일
I'm trying to write local functions in the way instructed in this video: https://www.youtube.com/watch?v=f0zKcuP3Wc0&list=PLlvaivyRxHmdTvpNBe4gKWrOBrcuxhpsl&index=48, beginning at 5:50. When running the code below, my MatLab R2022a says Unrecognized function or variable 'mymean'. What am I missing here?
x = 1:10;
n = length(x);
avg = mymean(x,n)
function a = mymean(v,n)
a = sum(v)/n;
  댓글 수: 6
Jonne Klockars
Jonne Klockars 2022년 6월 9일
The exact code is in the question. I have nothing else in the file. I select it all and press F9.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23 2022년 6월 9일
편집: Stephen23 2022년 6월 9일
"I select it all and press F9."
That will not work.
You need to run the script by calling it by name from the command line:
>> test
MATLAB is a bit more strict with function definitions than some younger languages around these days, where you can just define anything on the fly. Selecting function code and pressing F9 will not define the function and then let you call it. MATLAB simply does not work that way. You need to call the function or run the script that calls the function.
  댓글 수: 2
Jonne Klockars
Jonne Klockars 2022년 6월 9일
Also selecting the green play-symbol (Run) works. I watched the video series (link in question) before beginning the exercises and saw the instructor using F9. I'm quite sure she doesn't mention about the difference between it and Run / Command Line in any of the 60 videos.

댓글을 달려면 로그인하십시오.

추가 답변(0개)





Community Treasure Hunt

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

Start Hunting!

Translated by