Avoiding Repetitive Typing with Tab Completion
By Linda Chuss, MathWorks
Many MATLAB users save time by pressing the up arrow key while in the Command Window to recall typed lines. Not nearly as many users, however, take advantage of the Command Window's tab completion feature to avoid repetitive typing and remembering lengthy statements. Using examples, this article will detail the various ways you can use this feature to save even more time.
Tab completion in the MATLAB Command Window works like this. When you type the first few characters of the name of a function, variable, file, structure, or property and then press the Tab key, MATLAB completes the name for you. Basic examples presented first provide instructions for using tab completion. Further examples demonstrate the significant time-saving value of the feature.
How to Use Tab Completion: Basic Examples
To use tab completion, you first set the preferences. In Version 6.5 (Release 13), select File > Preferences > Command Window > Keyboard and Indenting. Under the Tab key, select the checkbox for Enable up to n tab completions. In Versions 6.1 (Release 12.1) and 6.0 (Release 12), instead select File > Preferences > Command Window to select the checkbox. Tab completion is not available for earlier versions of MATLAB.
In the Command Window, if you want to run the median
function, you do not need to type the entire function name. Type
med
and press Tab. MATLAB completes your typing with the only function name that starts with med
, by automatically adding the remaining letters. Your statement becomes
median
Next, you provide arguments to complete the statement and press Enter to run it. If MATLAB does not complete the name median
but instead moves the cursor to the right, you do not have the preference set for tab completion.
If you type fewer letters initially, for example
me
and press Tab, MATLAB does not automatically complete the name because there are multiple function names that start with me
. Instead, MATLAB beeps and waits for you to add more letters or request the list of possible completions. For example, you can add d
to make the entry unique. Your statement reads
med
Then press Tab to complete the name. Your statement becomes
median
Provide arguments and run the statement.
Instead of adding a unique character, you can press Tab again. MATLAB displays all possible completions. For this example, it shows all functions that start with me mean median memory menu mesh meshc meshz meshgrid methods methodsview mex mexext
After seeing all possible completions, type additional characters to make your entry unique, and press Tab. For example, type a
after me
, press Tab, and MATLAB completes the function as mean
If instead of showing a list of possible completions, MATLAB displays
There are over 10 completions.
type at least one more character to reduce the number of possible completions and press Tab again. The maximum number of possible completions that MATLAB displays is a preference you can set (see Preferences for Tab Completion). In this example, it is set to 10
.
Tab completion works anywhere in the line, not only at the beginning. For example, when you type
jan = med
and press Tab, MATLAB completes median
jan = median
and you can continue entering the statement.
Power of Tab Completion: Further Examples
You can use tab completion for more than just MATLAB functions, and you can use it multiple times while entering a statement.
Functions in Toolboxes
MATLAB completes function names in other MathWorks products. For example, if the Signal Processing Toolbox is installed, MATLAB does not automatically complete median
when you type med
. Instead, press Tab and MATLAB displays median
and medfilt1
as possible completions, where medfilt1
is a function in the Signal Processing Toolbox. The list of possible completions might include files that are not valid commands, including private functions.
M-Files in the Current Directory and on the Search Path
MATLAB completes the names of M-file functions and scripts that are in the current directory or in the MATLAB search path. If, for example, the file med_results
is in the current directory, when you type med
Tab MATLAB does not complete median
, but beeps. Press Tab again and MATLAB displays med_results
in addition to median
as possible completions.
Directories and Filenames
Tab completion works for directories and filenames in MATLAB functions. For example, type
addpath d:/myfTab
MATLAB completes the directory name, showing
addpath d:/myfiles
where myfiles
is a directory on your d drive. Press Tab twice, and MATLAB displays all myfiles
subdirectories as possible completions. You can continue using tab completion to display and complete directories until you finish the addpath
statement.
Variables in the Current Workspace
Type the first few letters of the name of a variable in the current workspace and press Tab. MATLAB completes the variable name for you, assuming there was only one possible completion. For example, type
janTab
MATLAB displays
jan_temps
where jan_temps
is a variable in the base workspace.
Tab completion can alert you to typographical errors. For example, if you type
janTab
and MATLAB beeps, press Tab again. MATLAB displays
jan_temps jant_emps
This might indicate you inadvertently created the variable jant_emps
.
Tab Completion for Structures
For structures, type up to and including the period separator in the name, and then press Tab. For example, type
mystructTab
to display all fields in mystruct
. If you type a structure name and include the start of a unique field after the period, pressing Tab completes that structure's field entry. For example, type
mystruct.nTab
MATLAB completes the entry, displaying
mystruct.name
where mystruct
contains no other fields that start with n
.
Tab Completion for Properties
Complete property names using tab completion, as in this graphics example. Here, f
is a figure. Type
set(f, 'papTabuTab,'cTab)
MATLAB completes the properties, including the closing quotes, displaying
set(f, 'paperunits','centimeters')
If MATLAB cannot complete the string with a unique property, MATLAB beeps. Press Tab again and MATLAB displays all possible property completions. For example, type
set(f, 'paTabTab
and MATLAB displays
PaperOrientation PaperPosition PaperPositionMode PaperSize PaperType PaperUnits Parent
Preferences for Tab Completion
Using preferences, you can turn tab completion off or specify how many possible completions MATLAB displays. Select File > Preferences > Command Window > Keyboard and Indenting. Tab size and tab completion preferences are under the Tab key section. In Versions 6.1 (Release 12.1) and 6.0 (Release 12), instead select File > Preferences > Command Window.
To turn tab completion off, clear the checkbox for Enable up to n
tab completions. With tab completion off, when you press Tab, MATLAB moves the cursor to the next tab stop, where the number of spaces in the tab is defined in the Tab size preference.
To specify how many possible completions MATLAB displays, enter a limit in the edit box, for example 10. When you type characters and press Tab, if there are 10 or fewer possible completions, MATLAB displays the entire list. If there are more than 10 possible completions, MATLAB instead displays
There are over 10 completions.
Type at least one more character and press Tab again to narrow the list of possible completions. When you set the value high, you can see many more possible completions at once. For example, Simulink users who set the value to 300 can see all get_param
completions at once. For some users, 300 completions might be too many to be useful. When you set the value relatively low, for example 10
, the message might alert you to an error. For example, if you meant to type
jaTab
but instead typed
daTab
MATLAB displays the message
There are over 10 completions.
Because you do not expect more than ten completions for ja
, you are aware of a possible error.
If you have not used tab completion, try it when you next use MATLAB. You might find yourself soon becoming a tab completion addict. If any of your colleagues are not aware of the feature, pass this tip along to them as well.
Published 2003