Having issues running a program with nested/shared functions
21 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!