function GlobalVars % Create a local variable w, assign a value to it, and then % see if the function Globalw can "see" it w = 23.5; Globalw pause % Now use the global command for a local variables x and y, assign % values to them, and then see if the function Globalxy can "see" them global x y x = 57.4; y = 89.1; Globalxy pause % Finally create a local variable z that we want to keep around between % calls to function GlobalVars, and see how z changes from one call of % GlobalVars to the next, and also if the function Globalz can "see" it. persistent z count first_time if isempty(first_time) z = 48.3; count = 1; first_time = false; else z = z+2; count = count+1; end fprintf('The current value of z is %4.1f\n', z) if count == 1 fprintf('and the program has been called 1 time.\n') else fprintf('and the program has been called %d times.\n', count) end pause Globalz %------------------------------------------------------------------------- function Globalw % First see if the variable x defined in GlobalVars can be % accessed by this function if exist('w') fprintf('The value of w is %4.1f in function Globalw.\n', w) else fprintf('No value has been assigned to w in function Globalw.\n') end %------------------------------------------------------------------------- function Globalxy % Next see if the variables x and y defined in GlobalVars can be % accessed by this function global x y if exist('x') & exist('y') fprintf('The value of x is %4.1f in function Globalxy\n', x) fprintf('The value of y is %4.1f in function Globalxy\n', y) else fprintf('No value has been assigned to x or y in function Globalxy.\n') end %------------------------------------------------------------------------- function Globalz % First see if the persistent variable z defined in GlobalVars can be % accessed by this function if exist('z') fprintf('The value of z is %4.1f in function Globalz.\n', z) else fprintf('No value has been assigned to z in function Globalz.\n') end