Script doesn't have access to local functions

Hi,
This seems like a silly problem, but I can't figure out what's going wrong. I'm trying to place code calling local functions into a script, which, from my understanding should look the same to Matlab as simply calling it directly. But for some reason it doesn't work. For example, the following code in the file LocalTest.m has the local function mymean, and works well.
function [a, s] = LocalTest(x)
a = mymean(x);
disp(['average = ',num2str(a)]);
end
function a = mymean(v)
a = mean(v);
end
But when calling the function content "a=mymean(x)" from a separate script file it doesn't work any more and produces the error:
Undefined function or variable 'mymean'. Error in LocalScript (line 1) a = mymean(x); Error in LocalTest (line 2) LocalScript;
function [a, s] = LocalTest(x)
LocalScript;
disp(['average = ',num2str(a)]);
end
function a = mymean(v)
a = mean(v);
end
Where LocalScript.m only contains
a = mymean(x);
The confusing thing is that this used to work, but I recently had to upgrade from 2013a to 2018a, and now it doesn't work any more...
Any help is greatly appreciated. Thanks!

 採用された回答

Steven Lord
Steven Lord 2018 年 4 月 6 日

3 投票

That was specifically mentioned in the release R2016b Release Notes. I believe this is related to the "local functions in scripts" feature.

1 件のコメント

Matthias
Matthias 2018 年 4 月 6 日
Ok, thanks for the link. This also gives the workaround, i.e., define a handle to the local function before calling the script and then use that handle from inside the script.
I.e., the main function would be changed to:
f_local = @mymean;
LocalScript;
and the script LocalScript.m then calls:
a = f_local(x);
Thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2018 年 4 月 6 日

コメント済み:

2018 年 4 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by