Global Variables

When you define a variable at the matlab prompt, it is defined inside of matlab's "workspace." Running a script does not affect this, since a script is just a collection of commands, and they're actually run from the same workspace. If you define a variable in a script, it will stay defined in the workspace.

Functions, on the other hand, do not share the same workspace. A function won't know what a variable is unless the it gets the variable as an argument, or unless the variable is defined as a variable that is shared by the function and the matlab workspace, or a global variable.

To use a global variable, every place (function, script, or at the matlab prompt) that needs to share that variable must have a line near the top identifying it as a global variable, ie:

global phi;
Then when the variable is assigned a value in one of those places, it will have a value in all the places that begin with the global statement.