Autocomplete of filenames for function input params

When using matlab functions such as "load" or "save" you can double-tab to autocomplete filenames that are in the path. However, when I have written my own functions that take filename strings as input this feature is not available (Which is reasonable since all functions shouldn't assume filename input).
I wonder if it is possible to somehow "hint" your own functions that they should try to autocomplete input parameters by searching over files in your matlab path.

1 个评论

Wow, this is *exactly* the same question I had. I haven't been able to figure it out, but I did look into existing matlab functions, and it offers no help.
For example, the 'importdata' function auto-completes file names, but it doesn't seem to do anything special to the input. It immediately puts the first argument into a variable called FileName, but I tried that and it doesn't seem to make a difference. I'd really like to know the answer to this as the auto-complete is super useful.

请先登录,再进行评论。

 采纳的回答

At some point between R2015a and R2016b, Matlab switched from using the TC.xml file to a more flexible and readable JSON implementation. Tab-completion information is now stored in a functionSignatures.json file located in the same directory as the relevant function's m-file. This also means you no longer have to hack Matlab root files in order to enjoy tab completion.
The function signatures file has the general form:
{
"FunctionName1":
{
"key1":"val1",
"key2":"val2",
"keyn":"valn"
},
"FunctionName2":
{
"key1":"val1",
"key2":"val2",
"keyn":"valn"
}
}
A number of keys are supported including "platform", "setsAns", "inputs", and "outputs", although inputs and outputs are by far the most common. Both of these keys take an array of objects that describe the input / output variable(s) and how to tab complete them.
The objects typically take one of the following forms:
{"name":"variable_name", "kind":"kind_option", "type":"string_or_array_of_type"}
{"mutuallyExclusiveGroup":
[
...
]
}
{"name":"varargin", "kind":"optional", "multiplicity":"append"}
The value for "kind" can include (but may not be limited to) "required", "optional", and "namevalue". The value for "type" can be a string such as "filepath" for tab completion of files, or a more complicated JSON array. Examples can be found in matlabroot/toolbox/matlab. It appears that matlabroot/toolbox/matlab/imagesci/functionSignatures.json has a particularly rich set of examples on how to do auto-completion. Here are entries for two functions that I cherry-picked from that file:
...
"h5read":
{
"inputs":
[
{"name":"filename", "kind":"required", "type":"filepath"},
{"name":"varargin", "kind":"optional", "multiplicity":"append"}
]
},
...
"imread":
{
"inputs":
[
{"mutuallyExclusiveGroup":
[
{"name":"filename", "kind":"required", "type":[["filepath=*.cur,*.ico,*.gif,*.hdf"], ["matlabpath=*.cur,*.ico,*.gif,*.hdf"]]},
[
{"name":"filename", "kind":"required", "type":[["char"], ["filepath"]]},
{"name":"fmt", "kind":"required", "type":["char", "choices={'cur','ico','gif','hdf'}"]}
]
]
},
{"name":"idx", "kind":"optional", "type":["numeric", "vector", ">=1"]}
],
"outputs":
[
{"name":"A", "type":[["numeric", "2d"], ["numeric", "3d"]]}
]
},
...

3 个评论

Thanks Walter - I added the official link to my blog post. It's about time they made it documented/supported - it's been around for a long time...

请先登录,再进行评论。

更多回答(4 个)

See Yair's article about tab-completion: Undocumented:Tab-Completeion
There you find instructions for editing:
edit(fullfile(matlabroot,'toolbox/local/TC.xml'))

4 个评论

Worked on 2011a, with the minor mod that TC.xml was write-protected by default
Worked on 2015a! The .json method did not
As Nicholas Mati answered below, Matlab switched from using TC.xml to the JSON mechanism around 2015-2016. The original question was made in 2011; my article about the undocumented TC.xml mechanism was posted in 2010. This worked well until 2015-ish, but for newer Matlab releases you need to use the JSON mechanism. Nothing lasts forever...

请先登录,再进行评论。

As of 2010a this was not possible at the user level. I have not heard any evidence that the situation has changed since.
Is there a way to use wildcards other than asterisks for filenames in a JSON block like
{"name":"filename", "kind":"required", "type":[["file=*.txt,*.asc,*.*n"], ["folder"], ["char"]]}
I ask because I want to match files with suffixes of the form filename.06n, where the two characters preceding the final "n" can be any digits, but not anything else. In UNIX I would use something like
file=*.txt,*.asc,*.*[0-9][0-9]n
... but that doesn't work at all in my functionSignatures.json.
A user hack is to use the ls function as a wrapper around your filename argument, and exploit the autocomplete of the ls function, as in
myFunction(ls('aFileName<tab>'), ...)
The ls function will autocomplete whatever file name you're looking for and return a string listing it.

2 个评论

This unfortunately only works if aFilename<tab> is located in the current folder. Also, it doesn't appear to support command syntax:
>> myFunction ls('aFileName<tab>')
I believe the limitation on current folder can be addressed by wrapping with
fullfile()
instead of
ls()
This should allow tab completion from anywhare on the filesystem and return the path and file exactly as tab-completed.
However, I don't think this addresses the limitation regarding command syntax.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by