Main Content

Check Variable Scope in Editor

Scoping issues can be the source of some coding problems. For instance, if you are unaware that nested functions share a particular variable, the results of running your code might not be as you expect. Similarly, mistakes in usage of local, global, and persistent variables can cause unexpected results.

The Code Analyzer does not always indicate scoping issues because sharing a variable across functions is not an error—it may be your intent. Use MATLAB®function and variable highlighting features to identify when and where your code uses functions and variables. If you have an active Internet connection, you can watch theVariable and Function Highlighting videofor an overview of the major features.

For conceptual information on nested functions and the various types of MATLAB variables, seeSharing Variables Between Parent and Nested FunctionsandShare Data Between Workspaces.

Use Automatic Function and Variable Highlighting

By default, the Editor indicates functions, local variables, and variables with shared scope in various shades of blue. Variables with shared scope include:global variables,persistent variables, and variables within nested functions. (For more information, seeNested Functions.)

To enable and disable highlighting or to change the colors, clickPreferencesand selectMATLAB>Colors>Programming tools. InMATLAB Online™, highlighting is enabled by default and changing the preferences for highlighting is not available.

By default, the Editor:

  • Highlights all instances of a given function or local variable in sky blue when you place the cursor within a function or variable name. For instance:

  • Displays a variable with shared scope in teal blue, regardless of the cursor location. For instance:

Example of Using Automatic Function and Variable Highlighting

Consider the code for a functionrowsum:

function rowTotals = rowsum % Add the values in each row and % store them in a new array x = ones(2,10); [n, m] = size(x); rowTotals = zeros(1,n); for i = 1:n rowTotals(i) = addToSum; end function colsum = addToSum colsum = 0; thisrow = x(i,:); for i = 1:m colsum = colsum + thisrow(i); end end end

When you run this code, instead of returning the sum of the values in each row and displaying:

ans = 10 10

MATLAB displays:

ans = 0 0 0 0 0 0 0 0 0 10

Examine the code by following these steps:

  1. On theHometab in theEnvironment部分中,点击Preferencesand selectMATLAB>Colors>Programming tools. Ensure thatAutomatically highlightandVariables with shared scopeare selected.

  2. Copy therowsumcode into the Editor.

    Notice the variableappears in teal blue, which indicatesiis not a local variable. Both therowTotalsfunction and theaddToSumfunctions set and use the variablei.

    The variablen, at line 6 appears in black, indicating that it does not span multiple functions.

  3. Hover the mouse pointer over an instance of variablei.

    A tooltip appears: The scope of variable 'i' spans multiple functions.

  4. Click the tooltip link for information about variables whose scope span multiple functions.

  5. Click an instance ofi.

    Every reference toi突出的年代ky blue and markers appear in the indicator bar on the right side of the Editor.

  6. Hover over one of the indicator bar markers.

    A tooltip appears and displays the name of the function or variable and the line of code represented by the marker.

  7. Click a marker to navigate to the line indicated in tooltip for that marker.

    This is particularly useful when your file contains more code than you can view at one time in the Editor.

Fix the code by changing the instance ofiat line 15 toy.

You can see similar highlighting effects when you click on a function reference. For instance, click onaddToSum.