1

I am a little confused about the variables' scope in a nested function in MATLAB.

  1. As nested function help docs explains: vaiable remains local to the nested function.
function main
   nestedfun1
   nestedfun2

   function nestedfun1
      x = 1;
   end

   function nestedfun2
      x = 2;
   end
save('maindata')
end

Does 'remains local' mean variables are deleted after calling nested function? Or keeping them in nested local workspace?

My opinion: main function calls nestedfun1 and nestfun2, but variables are local to nested function, so after these 2 nested functions are called, variable x is removed (deleted), so maindata.mat is empty, right?

  1. Sharing variables between parent and nested functions.
function main2
nestfun2

   function nestfun2
      x = 5;
   end 
   
x = x + 1;
end

main2 could be run successfully, but what I don't understand is:

a) call of nestfun2 is before x=x+1, why main function could be run? There is no define variable x when calling nestfun2.

b) Which line does MATLAB parse the function to see what variable will be created? x=5 or x=x+1? x=5 is earlier but it's in nested function.

c) Variable remains local, after calls nestfun2, why variable x=5 could be transferred to x=x+1?

d) If x=x+1 is deleted, main2 could also be run successfully. But if it's changed to just x, main2 produces an error (“Identifier 'x' is not a function or a shared variable”), why?

1 Answer 1

2

Yes, a local variable exists only during the time the function runs (unless it’s labeled persistent). A new workspace is created every time a function starts running, and this workspace is destroyed after. Each function has its own workspace, so that variables with the same name in different functions can be kept separate.

A nested function is a bit different in that it can access not only the variables in its own workspace, but also those in its parent’s workspace.

MATLAB’s variable scope is a bit different from languages such as Python or C. In those languages, variables exist from the moment they’re created until the end of their scope. In MATLAB, they exist during the full run of the function. MATLAB first parses the whole function, and compiles it. So at the start of the function, it already knows that x will be defined later. Names cannot change function during the execution of a function. You cannot use e.g. cos both as a function and as a variable. Something like

function bad
cos(5)
cos = 5;
end

produces an error because cos is understood to be a variable, not a function, during the full scope of the function. cos(5) is therefore indexing into a variable that has had nothing assigned to it yet.

This is what causes main2 to work the way it does: x is known to be a variable in this function’s scope (in its workspace) at the start of the function. Calling the nested function assigns to this variable, not to a local one within the nested function’s workspace, because it is already known to exist in the parent’s workspace.

Sign up to request clarification or add additional context in comments.

3 Comments

@Hamburger Yes, that is right. The only thing different for a nested function is that it will first look up a variable in its parent’s workspace before looking it up in its own workspace.
Each function has its own workspace, does this mean everyone nested function also has its own workspace? In 'function main', 'nestedfun1' has a veriable 'x', and 'nestedfun2' has another variable 'x' , they (variable 'x's in nestfun1 and nestedfun2 ) are in different workspaces. A variable could be defined later than call of nested function due to matlab will parse the code and determine which variable will be created. It seems like only '=' will creat variable, because in main2 if changed 'x=x+1' to just 'x' will be an error.
@Hamburger Yes, you need to assign something to a variable to create it. If it is not on the left hand side of the assignment operator, then it will be evaluated (its value will be fetched).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.