MATLAB won't run script unless functions are defined in a specific order?

30 ビュー (過去 30 日間)
LaraS
LaraS 2024 年 1 月 31 日
編集済み: Stephen23 2024 年 1 月 31 日
Hello,
Not sure what the best way to word this would be as I'm still familiarizing myself with MATLAB. I'm trying to run a code that our professor asked us to use for a class project. Even though all the functions have been defined within the script, some of them are called on before their "instance" further down the script. Because of this, MATLAB gives me an error that the function is not defined. I think it might have to do with the version of MATLAB that I'm running or my settings, because when my professor runs the code on his computer, it works just fine. I don't mind reorganizing things to make it make more sense to my script, just wanted to know if there's a smarter way to do this without having to reorganize things because I don't want to risk messing up with the code. I'm not sure if this makes sense at all but please feel free to ask for clarification. I'd paste the code for an example but it's really long because it's a multiprocess code and posting batches wouldn't really make my question make sense. I'm running R2023b Thanks in advance!
  3 件のコメント
Dinesh
Dinesh 2024 年 1 月 31 日
編集済み: Dinesh 2024 年 1 月 31 日
Function order doesn't matter in MATLAB. Function calls can be made before the definition of the function in the same file. Could you share a scaled down version of your code where you are able to reproduce the same error?
Stephen23
Stephen23 2024 年 1 月 31 日
編集済み: Stephen23 2024 年 1 月 31 日
"Function order doesn't matter in MATLAB."
It matters if they are anonymous functions or function handle definitions. The question does not specify if they are local functions or anonymous functions, which could make a difference.
fnh('hello world')
Unrecognized function or variable 'fnh'.
fnh = @disp;
@LaraS: please post the complete error message. This means all of the red text.

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2024 年 1 月 31 日
編集済み: Matt J 2024 年 1 月 31 日
As long as all your functions are placed at the bottom of the script, it shouldn't matter what order they are in.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by