| Contents | Index |
| On this page… |
|---|
Highlight Syntax to Help Ensure Correct Entries in the Editor Complete Names in the Editor Using the Tab Key |
By default, some language elements appear in different colors to help you better find matching elements, such as if/else statements. Similarly, unterminated strings have a different color than terminated strings. This is called syntax highlighting.

When you paste or drag a selection from the Editor to another application, such as Microsoft Word, the pasted text maintains the syntax highlighting colors and font characteristics from the Editor. MATLAB software pastes the selection to the Clipboard in RTF format, which many Microsoft Windows and Macintosh applications support.
To enable, disable, or change syntax highlighting preferences:
Select File > Preferences > Editor/Debugger > Languages.
From the Languages drop-down menu, select the language for which you want to set preferences.
Change the Syntax highlighting options, and then click OK.
MATLAB helps you automatically complete names as you type them in the Editor. Names supported include functions (including subfunctions and nested functions within the active document), models, class and package names that are on the MATLAB search path or in the current folder, variables (including structures and objects), and Handle Graphics properties.
To complete names in the Editor:
Select File > Preferences > Keyboard.
Select Enable in Editor/Debugger.
Type the first few characters of the name you want to complete, and then press the Tab key.
MATLAB completes the name or offers a list of names from which you can select one.
Do one the following, depending on how MATLAB responds. If MATLAB:
Completes the name you want
You are done.
Presents a list of possible matches
To select the name you want, use the arrow keys, and then press the Tab key. See Example of Name Completion in the Editor for a Variable.
To clear the list without selecting anything, press the Esc (escape) key.
To narrow a long list before making a selection, add additional characters to your original term.
Completes the first part of a name that uses dot notation.
In the Editor, type a dot, and then press the Tab key. Repeat until the name is complete. Example of Name Completion in the Editor for a Structure.
Completes one property of several
In the Editor, type a comma and the first part of the next property, and the press the Tab key. Repeat for all properties you want to specify. See Example of Name Completion in the Editor for Figure Properties.
Be aware that MATLAB
Completes nested functions only when they are available at the current location of the cursor.
Completes names of variables and properties of figures that are in the current workspace.
The current workspace appears in the Stack on the toolbar.
Completes names of variables defined in the active document. The variable must be valid at the current location of the cursor (that is, already defined).
Does not complete the field names of structure arrays defined only within the active file.
Does not complete method or property names for objects defined only within the active file.
For more information, see the following examples:
This example shows how to use tab completion for a variable name when there is more than one name that starts with the characters you type. The process is similar for other names that do not use dot notation.
Select File > Preferences > Keyboard.
Select Enable in Editor/Debugger.
In the Command Window create a variable, costs_march, by typing the following, and then pressing Enter.
costs_march = 100;
In the Editor, type cos, and then press the Tab key.
The resulting list of possible completions includes the variable name you created, costs_march, and functions and models that begin with cos.

Navigate the list of possible completions using the up and down arrow keys until costs_march is selected.
Choose costs_march by pressing the Tab key.
MATLAB completes the name in the Editor.
This example shows how to use tab completion for a structure that is in the current workspace. The process is similar for other names that use dot notation.
Select File > Preferences > Keyboard.
Select Enable in Editor/Debugger.
In the Command Window create a structure, description, by typing the following, and then pressing Enter.
description = struct('type', {'big','little'}, 'color', {'red'}, ...
'x', {3 4})In the Editor, type descrip, and then press the Tab key.
MATLAB completes the name description.
In the Editor, type a dot, ., after description, and then press the Tab key.
MATLAB displays all fields of description: color, type, and x.
Navigate the list of possible completions using the up and down arrow keys until type is selected, and then press the Tab key.
The Editor contains description.type.
This example shows how to use tab completion for a figure that is in the current workspace.
Select File > Preferences > Keyboard.
Select Enable in Editor/Debugger.
In the Command Window create a figure, myfig, by typing the following, and then pressing Enter.
f = figure('Name','Simulation Plot Window','NumberTitle','off')In the Editor, type set(f, 'pap, and then press the Tab key.
The Editor displays

Select the PaperUnits property from the list by typing an additional letter, u, which makes the string in the Editor unique.
MATLAB completes the property, including the closing quote: set(f, 'paperunits'
Continue adding to the statement by typing , 'c, and the press the Tab key
MATLAB automatically completes the property, centimeters, because it is the only possible completion.
If the preference for tab completion is selected, and you want to use the Tab key to add spacing within your statements also, add a space before pressing Tab. For example, to create this statement
if a=mate %test input value
add a space after mate and then press Tab. If you do not include the space, the following happens instead:
if a=material
This is because the tab completion feature automatically causes mate to complete as the material function.
Alternatively, turn off the tab completion preference to use Tab for spacing in the Editor.
MATLAB Code Analyzer can automatically check your code for coding problems, as described in the following sections:
You can view warning and error messages about your code, and modify your file based on the messages. The messages update automatically and continuously so you can see if your changes addressed the issues noted in the messages. Some messages offer additional information, automatic code correction, or both.
To use continuous code checking in a MATLAB code file in the Editor:
Select File > Preferences > Code Analyzer, and then select the Enable integrated warning and error messages check box.
Set the Underlining option to Underline warnings and errors, and then click OK.
Open a MATLAB code file in the Editor. This example uses the sample file lengthofline.m that ships with the MATLAB software:
Open the example file:
open(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m'))
Save the example file to a folder to which you have write access. For the example, lengthofline.m is saved to C:\my_MATLAB_files.
Examine the message indicator at the top of the message bar to see the Code Analyzer messages reported for the file:
Red indicates syntax errors were detected. Another way to detect some of these errors is using syntax highlighting to identify unterminated strings, and delimiter matching to identify unmatched keywords, parentheses, braces, and brackets.
Orange indicates warnings or opportunities for improvement, but no errors, were detected.
Green indicates no errors, warnings, or opportunities for improvement were detected.
In this example, the indicator is red, meaning that there is at least one error in the file.

Click the message indicator to go to the next code fragment containing a message. The next code fragment is relative to the current cursor position, viewable in the status bar.
In the lengthofline example, the first message is at line 22. The cursor moves to the beginning of line 22.
The code fragment for which there is a message is underlined in either red for errors or orange for warnings and improvement opportunities.
View the message by moving the mouse pointer within the underlined code fragment.
The message opens in a tooltip and contains a Details button that provides access to additional information by extending the message. Not all messages have additional information.

Click the Details button.
The window expands to display an explanation and user action.

The message indicator and underlining automatically update to reflect changes you make, even if you do not save the file.
On line 28, hover over prod.
The code is underlined because there is a warning message, and it is highlighted because an automatic fix is available. When you view the message, it provides a button to apply the automatic fix.

Fix the problem by doing one of the following:
If you know what the fix is (from previous experience), click Fix.
If you unfamiliar with the fix, view, and then apply it as follows:
Right-click the highlighted code (for a single-button mouse, press Ctrl+ click), and then view the first item in the context menu.
Click the fix.
MATLAB automatically corrects the code.
In this example, MATLAB replaces prod(size(hline)) with numel(hline).

Go to a different message by doing one of the following:
To go to the next message, click the message indicator or the next underlined code fragment.
To go to a line that a marker represents, click a red or orange line in the indicator bar .
To see the first error in lengthofline, click the first red marker in the message bar. The cursor moves to the first suspect code fragment in line 48. The Details and Fix buttons are dimmed, indicating that there is no more information about this message and there is no automatic fix.

Multiple messages can represent a single problem or multiple problems. Addressing one might address all of them, or after addressing one, the other messages might change or what you need to do might become clearer.
Modify the code to address the problem noted in the message—the message indicators update automatically.
In the lengthofline, the message suggests a delimiter imbalance. You can investigate that as follows:
Select File > Preferences > Keyboard.
Under Delimiter Matching, select Match on arrow key, and then click OK.
In the Editor, move the arrow key over each of the delimiters to see if MATLAB indicates a mismatch.
In the example, it might appear that there are no mismatched delimiters. However, code analysis detects the semicolon in parentheses: data{3}(;), and interprets it as the end of a statement. The message reports that the two statements on line 48 each have a delimiter imbalance.
In line 48, change data{3}(;) to data{3}(:).
Now, the underline no longer appears in line 48. The single change addresses the issues in both of the messages for line 48.
Because the change removed the only error in the file, the message indicator at the top of the bar changes from red to orange, indicating that only warnings and potential improvements remain.
After modifying the code to address all the messages, or disabling designated messages, the message indicator becomes green. The example file with all messages addressed has been saved as lengthofline2.m. Open the corrected example file with the command:
open(fullfile(matlabroot,'help','techdoc',...
'matlab_env', 'examples','lengthofline2.m'))
You can create a report messages for an individual file, or for all files in a folder, using one of these methods:
Run a report for an individual MATLAB code file:
From a file in the Editor, select Tools > Code Analyzer > Show Code Analyzer Report.
Modify your file based on the messages in the report.
Save the file.
Rerun the report to see if your changes addressed the issues noted in the messages.
Run a report for all files in a folder:
In the Current Folder browser, click the Actions button
.
Select Reports > Code Analyzer Report.
Modify your files based on the messages in the report.
For details, see Using the MATLAB Code Analyzer Report.
Save the file.
Rerun the report to see if your changes addressed the issues noted in the messages.
Depending on the stage at which you are in completing a MATLAB file, you might want to restrict the code underlining. You can do this by using the Code Analyzer preference referred to in step 1, in Avoid Mistakes While Editing Code. For example, when first coding, you might prefer to underline only errors because warnings would be distracting.
Code analysis does not provide perfect information about every situation and in some cases, you might not want to change the code based on a message. If you do not want to change the code, and you do not want to see the indicator and message for that line, suppress them. For the lengthofline example, in line 49, the first message is Terminate statement with semicolon to suppress output (in functions). Adding a semicolon to the end of a statement suppresses output and is a common practice. Code analysis alerts you to lines that produce output, but lack the terminating semicolon. If you want to view output from line 49, do not add the semicolon as the message suggests.
There are a few different ways to suppress (turn off) the indicators for warning and error messages:
You cannot suppress error messages such as syntax errors. Therefore, instructions on suppressing messages do not apply to those types of messages.
Suppress an Instance of a Message in the Current File. You can suppress a specific instance of a Code Analyzer message in the current file. For example, using the code presented in Avoid Mistakes While Editing Code, follow these steps:
In line 49, right-click at the first underline (for a single-button mouse, press Ctrl+click).
From the context menu, select Suppress 'Terminate statement with semicolon...' > On This Line.
The comment %#ok<NOPRT> appears at the end of the line, which instructs MATLAB not to check for a terminating semicolon at that line. The underline and mark in the indicator bar for that message disappear.
If there are two messages on a line that you do not want to display, right-click separately at each underline and select the appropriate entry from the context menu.
The %#ok syntax expands. For the example, in the code presented in Avoid Mistakes While Editing Code, ignoring both messages for line 49 adds %#ok<NBRAK,NOPRT>.
Even if Code Analyzer preferences are set to enable this message, the specific instance of the message suppressed in this way does not appear because the %#ok takes precedence over the preference setting. If you later decide you want to check for a terminating semicolon at that line, delete the %#ok<NOPRT> string from the line.
Suppress All Instances of a Message in the Current File. You can suppress all instances of a specific Code Analyzer message in the current file. For example, using the code presented in Avoid Mistakes While Editing Code, follow these steps:
In line 49, right-click at the first underline (for a single-button mouse, press Ctrl+click).
From the context menu, select Suppress 'Terminate statement with semicolon...' > In This File.
The comment %#ok<*NOPRT> appears at the end of the line, which instructs MATLAB to not check for a terminating semicolon throughout the file. All underlines, as well as marks in the message indicator bar that correspond to this message disappear.
If there are two messages on a line that you do not want to display anywhere in the current file, right-click separately at each underline, and then select the appropriate entry from the context menu. The %#ok syntax expands. For the example, in the code presented in Avoid Mistakes While Editing Code, ignoring both messages for line 49 adds %#ok<*NBRAK,*NOPRT>.
Even if Code Analyzer preferences are set to enable this message, the message does not appear because the %#ok takes precedence over the preference setting. If you later decide you want to check for a terminating semicolon in the file, delete the %#ok<*NOPRT> string from the line.
Suppress All Instances of a Message in All Files. You can disable all instances of a Code Analyzer message in all files. For example, using the code presented in Avoid Mistakes While Editing Code, follow these steps:
In line 49, right-click at the first underline (for a single-button mouse, press Ctrl+click).
From the context menu, select Suppress 'Terminate statement with semicolon...' > In All Files.
This modifies the Code Analyzer preference setting.
If you know which message or messages you want to suppress, you can disable them directly using Code Analyzer preferences, as follows:
Select File > Preferences > Code Analyzer.
Filter the messages to find the ones you want to suppress.
Clear the check box associated with each message you want to suppress in all files.
Click OK.
Save and Reuse Code Analyzer Message Settings. You can specify that you want certain Code Analyzer messages enabled or disabled, and then save those settings to a file. When you want to use a settings file with a particular file, you select it from the Code Analyzer preferences pane. That setting file remains in effect until you select another settings file. Typically, you change the settings file when you have a subset of files for which you want to use a particular settings file.
Follow these steps:
Select File > Preferences > Code Analyzer
The Preferences dialog box opens and displays the Code Analyzer preferences pane.
Enable or disable specific messages, or categories of messages.
Click the Actions button
, select Save as,
and then save the settings to a txt file.
Click OK.
You can reuse these settings for any MATLAB file, or provide the settings file to another user.
To use the saved settings:
In the Editor, select Tools > Code Analyzer.
The currently selected setting choice displays, preceded by a bullet point.
Choose from any of the settings files, such as the My_Settings example, as shown here.
The settings you choose are in effect for all MATLAB files until you select another set of Code Analyzer settings.

If you receive code that contains suppressed messages, you might want to review those messages without the need to unsuppress them first. A message might be in a suppressed state for any of the following reasons:
One or more %#ok<message-ID> directives are on a line of code that elicits a message specified by <message-ID>.
One or more %#ok<*message-ID> directives are in a file that elicits a message specified by <message-ID>.
It is cleared in the Code Analyzer preferences pane.
It is disabled by default.
To determine the reasons why some messages are suppressed:
Search the file for the %#ok directive and create a list of all the message IDs associated with that directive.
Open the Code Analyzer preferences dialog box by selecting File > Preferences > Code Analyzer.
In the filter field, type msgid: followed by one of the message IDs, if any, you found in step 1.
The message list now contains only the message that corresponds to that ID. If the message is a hyperlink, click it to see an explanation and suggested action for the message. This can provide insight into why the message is suppressed or disabled. The following image shows how the Preferences dialog box appears when you enter msgid:CPROP in the filter field.

Click the
button to clear the filter
field, and then repeat step 3 for each message ID you found in step
1.
Click the
button to clear the filter
field.
Filter the messages to display those that are disabled by default and disabled in the Preferences pane by clicking the down arrow to the right of the filter field. Then, click Show Disabled Messages.
Review the message associated with each message ID to understand why it is suppressed in the code or disabled in Preferences.
Code analysis is a valuable tool, but there are some limitations:
Sometimes, it fails to produce Code Analyzer messages where you expect them.
By design, code analysis attempts to minimize the number of incorrect messages it returns, even if this behavior allows some issues to go undetected.
Sometimes, it produces messages that do not apply to your situation.
When provided with message, click the Detail button for additional information, which can help you to make this determination. Error messages are almost always problems. However, many warnings are suggestions to look at something in the code that is unusual and therefore suspect, but might be correct in your case.
Suppress a warning message if you are certain that the message does not apply to your situation. If your reason for suppressing a message is subtle or obscure, include a comment giving the rationale. That way, those who read your code are aware of the situation.
For details, see Adjust Code Analyzer Message Indicators and Messages.
These sections describe code analysis limitations with respect to the following:
Distinguish Function Names from Variable Names. Code analysis cannot always distinguish function names from variable names. For the following code, if the Code Analyzer message is enabled, code analysis returns the message, Code Analyzer cannot determine whether xyz is a variable or a function, and assumes it is a function. Code analysis cannot make a determination because xyz has no obvious value assigned to it. However, the program might have placed the value in the workspace in a way that code analysis cannot detect.
function y=foo(x) . . . y = xyz(x); end
For example, in the following code, xyz can be a function, or can be a variable loaded from the MAT-file. Code analysis has no way of making a determination.
function y=foo(x)
load abc.mat
y = xyz(x);
endVariables might also be undetected by code analysis when you use the eval, evalc, evalin, or assignin functions.
If code analysis mistakes a variable for a function, do one of the following:
Initialize the variable so that code analysis does not treat it as a function.
For the load function, specify the variable name explicitly in the load command line. For example:
function y=foo(x)
load abc.mat xyz
y = xyz(x);
endDistinguish Structures from Handle Objects. Code analysis cannot always distinguish structures from handle objects. In the following code, if x is a structure, you might expect a Code Analyzer message indicating that the code never uses the updated value of the structure. If x is a handle object, however, then this code can be correct.
function foo(x) x.a = 3; end
Code analysis cannot determine whether x is a structure or a handle object. To minimize the number of incorrect messages, code analysis returns no message for the previous code, even though it might contain a subtle and serious bug.
Distinguish Built-In Functions from Overloaded Functions. Code analysis does not use the MATLAB path information because it can be different, depending on whether you are editing or running the program. If some built-in functions are overloaded in a class or on the path, Code Analyzer messages might apply to the built-in function, but not to the overloaded function you are calling. In this case, suppress the message on the line where it appears or suppress it for the entire file.
For information on suppressing messages, see Adjust Code Analyzer Message Indicators and Messages.
Determine the Size or Shape of Variables. Code analysis has a limited ability to determine the type of variables and the shape of matrixes. Code analysis might produce messages that are appropriate for the most common case, such as for vectors. However, these messages might be inappropriate for less common cases, such as for matrixes.
Analyze Class Definitions with Superclasses. Because code analysis looks at one file at a time and does not use the path, it has no way to analyze superclasses. Therefore, the amount of checking that code analysis can provide for a class definition with superclasses is limited. In general, code analysis cannot always tell whether the class is a handle class. It makes an educated guess, but often cannot get enough information, to make a determination for certain.
Analyze Class Methods. Most class methods must contain at least one argument that is an object of the same class as the method. But it does not always have to be the first argument. When it is, code analysis can determine that an argument is an object of the class you are defining, and it can do various checks. For example, it can check that the property and method names exist and are spelled correctly. However, when code analysis cannot determine that an object is an argument of the class you are defining, then it cannot provide these checks.
You can switch between showing or hiding Compiler deployment messages when you work on a file. Change the Code Analyzer preference for this message category. Your choice likely depends on whether you are working on a file to be deployed. When you change the preference, it also changes the setting in the Editor. The converse is also true—when you change the setting from the Editor, it effectively changes this preference. However, if the dialog box is open at the time you modify the setting in the Editor, you will not see the changes reflected in the Code Analyzer preferences dialog box . Whether you change the setting from the Editor or from the Code Analyzer preferences dialog box, it applies to the Editor and to the Code Analyzer Report.
To enable MATLAB® Compiler™ deployment messages:
Select File > Preferences > Code Analyzer.
Click the down arrow next to the filter field, and then select Show Messages in Category > MATLAB Compiler (deployment) messages.
Click the Enable Category button.
Clear individual messages that you do not want to display for your code (if any).
Decide if you want to save these settings, so you can reuse them next time you work on a file to be deployed.
The settings txt file, which you can create as described in Save and Reuse Code Analyzer Message Settings, includes the status of this setting.
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 the Variable and Function Highlighting video demo for an overview of the major features.
For conceptual information on nested functions and the various types of MATLAB variables, see Variable Scope in Nested Functions and Types of Variables.
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, see Scope in Nested Functions.)
To enable and disable highlighting or to change the colors, select File > Preferences > Colors > Programming tools.
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:
![]()
Consider the code for a function rowsum:
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
endWhen you run this code, instead of returning the sum of the values in each row and displaying:
ans =
10 10MATLAB displays:
ans =
0 0 0 0 0 0 0 0 0 10Examine the code by following these steps:
Select File > Preferences > Colors > Programming Tools, and make sure Automatically Highlight and Variables with shared scope are selected.
Copy the rowsum code into the Editor.
Notice the variable
appears in teal blue, which indicates i is
not a local variable. Both the rowTotals function
and the addToSum functions set and use the variable i.
The variable n, at line 6 appears in black, indicating that it does not span multiple functions.

Hover the mouse pointer over an instance of variable i.
A tooltip appears: The scope of variable 'i' spans multiple functions.
Click the tooltip link for information about variables whose scope span multiple functions.
Click an instance of i.
Every reference to i highlights in sky blue and markers appear in the indicator bar on the right side of the Editor. The status bar displays:
5 usages of i found

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.
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 of i at line 15 to y.
You can see similar highlighting effects when you click on a function reference. For instance, click on addToSum.
![]() | Evaluate Subsections of Files Using Code Cells | Debugging Process and Features | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |