3.0

3.0 | 1 rating Rate this file 24 downloads (last 30 days) File Size: 6.04 KB File ID: #25217

Intelligent code completion for matlab

by Leif Persson

 

03 Sep 2009 (Updated 09 Oct 2009)

Code covered by BSD License  

Code completion for the Matlab editor.

Download Now | Watch this File

File Information
Description

The Matlab editor provides an integrated code completion through the tab key. However the scope of this completion is the entire Matlab path, so the number of matches is usually large when matching a few characters. On the other hand the native Matlab code completion is sometimes too restrictive since it only matches sequential characters. I have found the more flexible matching of TextMate very convenient to use, where e.g. the string 'fcn' can be completed to 'function'. The purpose of this macro is to provide such a more flexible matching algorithm, but with scope restricted to the current document.
To use this macro you must install EditorMacro by Yair Altman, put this file (Matlab_Code_Completion_Macro.m) on your Matlab path and add the following lines to your startup.m file:
macros = EditorMacro('ctrl-space',@Matlab_Code_Completion_Macro, 'run');
 macros = EditorMacro('ctrl-alt-space',@Matlab_Code_Completion_Macro, 'run');
 macros = EditorMacro('shift-ctrl-space',@Matlab_Code_Completion_Macro, 'run');
To use the macro, type in the Matlab editor a string to be completed, e.g.,
fo
To find possible completions, press ctrl-space.
Then you get the suggested code completion
fo<<--{for}
Pressing ctrl-space once again you get the next best completion
fo<<--{function}
and pressing ctrl-space again
fo<<--{parfor}
Pressing ctrl-shift-space we step back to the previous suggestion
fo<<--{function}
and this completion is chosen by pressing ctrl-alt-space, so we get
function
Now with the caret positioned at the end of the function keyword, we press ctrl-alt-space again and get
function [
Now we can type a variable list
function [y,z
and type ctrl-alt-space again to get
function [y,z] =
Type the function name
function [z,w] = f
and press ctrl-alt-space again
function [z,w] = f(
Type a variable list
function [z,w] = f(x,y
and press ctrl-alt-space again
function [z,w] = f(x,y)
Next, you can type the function body, for example
function [z,w] = f(x,y)
       z=x+y; w=z-y;
(to get correct indentation you may need to apply the smart indent, e.g., by the shortcut ctrl-i). Finally, you can use ctrl-alt-space again to get the end statement
function [z,w] = f(x,y)
       z=x+y; w=z-y;
end
Similar completions work for if, elseif, for and while.

In conclusion, I have found this macro a valuable complement to the native Matlab code completion, and it has saved me a lot of typing and searching for variable and function names.
I hope you find it useful. Enjoy!

MATLAB release MATLAB 7.8 (R2009a)
Other requirements Requires EditorMacro Tested with Matlab R2007b, R2008a, R2008b, R2009a, R2009b
Zip File Content  
Other Files license.txt,
Matlab_Code_Completion_Macro.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
08 Oct 2009 Les

Leif:

I tried your utility and was quite impressed at first. However, it seems that it breaks tab completion for workspace variables in the command window. So, I won't be using it.

You might want to check into this and see if you can fix it.

If you find a fix I might try this again.

Note also that the suggested startup.m commands in the comment header don't work. I had to remove the single quote at the end of the function handle. Here is what I used:

macros = EditorMacro('ctrl-space',@Matlab_Code_Completion_Macro, 'run'); %#ok<NASGU>
macros = EditorMacro('ctrl-alt-space',@Matlab_Code_Completion_Macro, 'run'); %#ok<NASGU>
macros = EditorMacro('shift-ctrl-space',@Matlab_Code_Completion_Macro, 'run');

Regards
Les

09 Oct 2009 Leif Persson

Les,
thanks for pointing out the documentation error.
I have tested with R2007b-R2009b on Win XP x64 but I couldn't
reproduce the error - tab completion in the command window
seems to work fine on my computer for all these releases.

Matlab_Code_Completion_Macro doesn't work properly in 2007a though, it seems that something in the regexp semantics changed from R2007a to R2007b.

Please login to add a comment or rating.
Updates
04 Sep 2009

Improved code completion: function and variable names within comments are now ignored

04 Sep 2009

Bug fix

07 Sep 2009

Improved sorting of code completion matches
Bugfix for comment exclusion
Added end statement as last altenative for ctrl-alt

07 Sep 2009

Cleaned up debug prints

07 Sep 2009

Cleaned up debug prints

07 Sep 2009

Tokens in strings now excluded
Bug fixes

07 Sep 2009

Bug fix

10 Sep 2009

Turned off debug print (again!)

10 Sep 2009

Bugfix for struct fields

09 Oct 2009

Updated documentation

09 Oct 2009

Description update

Tag Activity for this File
Tag Applied By Date/Time
matlab editor Leif Persson 03 Sep 2009 10:24:52
code completion Leif Persson 03 Sep 2009 10:24:52
code snippets Leif Persson 03 Sep 2009 10:24:52
code completion Chris Rodgers 08 Sep 2009 09:40:39
autocompletion Leif Persson 17 Sep 2009 04:38:08
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com