We all know MATLAB provides tab-completion for filenames used as arguments in MATLAB function like importdata
,imread
. How do we do that for the functions we create?
EDIT:
Displays the files and folders in the current directory.
Caution: unsupported hack here.
Take a look at the file toolbox\local\TC.xml in your Matlab installation. This appears to contain the argument type mappings for tab completion. (I found this by just grepping the Matlab installation for "imread" in R2009b.)
Adding this line inside the <TC>
element will get you tab-completion of file names for each of its arguments.
<binding name="importdata" ctype="FILE"/>
This requires modifying the Matlab installation in place. For some of the other files in toolbox/local, like classpath.txt, you can override them by placing modified copies in the directory you start Matlab from, but this didn't work for TC.xml for me.
There is no supported way to add your functions to the argument Tab completion, but one trick I use is to put a "!" in front of the command so it is treated like a system command. Tab will then complete file paths. Once my MATLAB command is done, I home to the beginning of the line, delete the "!" and press enter.
For Matlab 2016a and above :
The file Tc.xml is not present in Matlab 2016a onwards. It uses a .json (Java Script Object Notation) file to achieve the same. A simple example of this can be as follows.
Suppose you have a Matlab function file called myFunction.m
. Furthermore, suppose that this function needs files with extension .ext
as input and you want the tab-completion to show all possible input options for this function. Then, write the following content in a file and name it functionSignatures.json
.
{
"myFunction":
{
"inputs":
[
{"name":"filename", "kind":"required", "type":"filepath=*.ext" }
]
}
}
Place this file in the same directory as myFunction.m
file. Now, restart Matlab.
What this file does : While typing in the function input, upon pressing tab, you will see a list of files with the extension .ext
popping up. If you want all the files to be shown in tab completion popup irrespective of their extension, then replace "type":"filepath=*.ext"
with "type":"filepath"
in the file functionSignatures.json
.
来源:https://stackoverflow.com/questions/1842804/tab-completion-of-filenames-as-arguments-for-matlab-scripts