Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP - MATLAB How do I save the X and Y variables to the workspace for the neste

ID: 673744 • Letter: H

Question

HELP - MATLAB

How do I save the X and Y variables to the workspace for the nested function? When I run UpdateBoard, it tells me that X and Y are undefined variables.

function [] = UpdateBoard(playernum, idx)

if playernum == 1

linear2XY(idx);

   plot([X X], [Y Y], 'gs', 'MarkerSize', 75)

else

linear2XY(idx);

plot([X X], [Y Y], 'r.', 'MarkerSize', 75)

end

function [X, Y] = linear2XY(linear)

if linear == 1

X = 0.5;

Y = 2.5;

elseif linear == 2

X = 0.5;

Y = 1.5;

elseif linear == 3

X = 0.5;

Y = 0.5;

elseif linear == 4

X = 1.5;

Y = 2.5;

elseif linear == 5

X = 1.5;

Y = 1.5;

elseif linear == 6

X = 1.5;

Y = 0.5;

elseif linear == 7

X = 2.5;

Y = 2.5;

elseif linear == 8

X = 2.5;

Y = 1.5;

else

X = 2.5;

Y = 0.5;

end

end

end

Explanation / Answer

Call the update1 function from the command line and assign to variable Y in the base workspace:

Nested Functions

A nested function has access to the workspaces of all functions in which it is nested. So, for example, a nested function can use a variable (in this case, x) that is defined in its parent function:

When parent functions do not use a given variable, the variable remains local to the nested function. For example, in this version of primaryFx, the two nested functions have their own versions of x that cannot interact with each other.

For more information, see Nested Functions.

Persistent Variables

When you declare a variable within a function as persistent, the variable retains its value from one function call to the next. Other local variables retain their value only during the current execution of a function. Persistent variables are equivalent to static variables in other programming languages.

Declare variables using the persistent keyword before you use them. MATLAB® initializes persistent variables to an empty matrix, [].

For example, define a function in a file named findSum.m that initializes a sum to 0, and then adds to the value on each iteration.

When you call the function, the value of SUM_X persists between subsequent executions.

These operations clear the persistent variables for a function:

clear all

clear functionname

Editing the function file

To prevent clearing persistent variables, lock the function file using mlock.

Global Variables

Global variables are variables that you can access from functions or from the command line. They have their own workspace, which is separate from the base and function workspaces.

However, global variables carry notable risks. For example:

Any function can access and update a global variable. Other functions that use the variable might return unexpected results.

If you unintentionally give a "new" global variable the same name as an existing global variable, one function can overwrite the values expected by another. This error is difficult to diagnose.

Use global variables sparingly, if at all.

If you use global variables, declare them using the global keyword before you access them within any particular location (function or command line). For example, create a function in a file called falling.m:

Then, enter these commands at the prompt:

The two global statements make the value assigned to GRAVITY at the command prompt available inside the function. However, as a more robust alternative, redefine the function to accept the value as an input:

Then, enter these commands at the prompt:

Evaluating in Another Workspace

The evalin and assignin functions allow you to evaluate commands or variable names from strings and specify whether to use the current or base workspace.

Like global variables, these functions carry risks of overwriting existing data. Use them sparingly.

evalin and assignin are sometimes useful for callback functions in graphical user interfaces to evaluate against the base workspace. For example, create a list box of variable names from the base workspace: