Having issues running a program with nested/shared functions

조회 수: 21 (최근 30일)
Chris Keightley
Chris Keightley 2021년 5월 20일
답변: Walter Roberson 2021년 5월 20일
Hello everyone,
I am currently trying to run the following code in MATLAB to create an N-back task. The code is from 2019. I am recieving the following error, and I'm not sure what it's saying to "initialize it within the current scope". Any help would be greatly appreciated, attached below is the file.
Error:File: nback_matlab.m Line: 116 Column: 33
Identifier 'nback' is not a function or a shared variable. To share 'nback' with a nested function, initialize it in the current scope. For more
information, see Sharing Variables Between Parent and Nested Functions.

답변 (1개)

Walter Roberson
Walter Roberson 2021년 5월 20일
You initialize nback in get_defaults through a simple assignment. Except in special constructions, that would be an assignment to a plain workspace variable, and plain workspace variables are removed when the function returns. A plain assignment to a variable within a function never creates a new variable within the context of a calling function.
get_defaults is called within a settings function, so after get_defaults is executed inside the settings function, the variables assigned to inside that function do not exist inside the setting function, and certainly they do not also get copied up to the function that calls the settings function.
The "special construction" that I mentioned before:
If you have an outer function that assigns to a variable, and then syntactically after the variable has been assigned to, the outer function defines a second function inside the outer function (without terminating the outer function!), then when the inner function is called, the inner function can read and write the variable, and the change will be seen in the outer function and any inside functions. For example if you had
function outer
nback = -1;
function get_defaults
nback = 2;
brolly = 3;
end %of inner function
brolly = -2;
get_defaults();
disp(nback) %2 as set in get_defaults
disp(brolly) %-2 as set in outer
end %of outer function
Only variables assigned to before the inner function is defined are treated as shared, so brolly is not shared but nback is shared.
Now... back when shared variables were introduced, matlab treated them as shared if they were assigned to before the nested function was called. At the time get_defaults is called, brolly has been assigned to, and matlab said that was good enough to treat it as shared; after a few releases it was changed to require initialization before the sharing function is defined.
I do not recall at the moment when the change was made. My memory is suggesting r2008b, but it could have been r2010a... it would have to be researched.
But it would not matter for your purposes because your code does not assign to nback before the settings routine is called either.
What does it mean in practice:
Notice the section that talks about initializing global variables. Add an assignment to nback there. Any value is fine including [].
You will need to look through the rest of the variables set in get_defaults and assign to them in the same section, such as the background color scheme variable.

카테고리

Help CenterFile Exchange에서 Psychology에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by