Autocomplete of filenames for function input params

62 vues (au cours des 30 derniers jours)
Miro
Miro le 8 Juin 2011
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 commentaire
Luke Winslow
Luke Winslow le 24 Juin 2011
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.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 24 Juin 2011
Modifié(e) : Jan le 17 Juil 2017
See Yair's article about tab-completion: Undocumented:Tab-Completeion
There you find instructions for editing:
edit(fullfile(matlabroot,'toolbox/local/TC.xml'))
  4 commentaires
Tyvaughn Holness
Tyvaughn Holness le 9 Mar 2020
Worked on 2015a! The .json method did not
Yair Altman
Yair Altman le 11 Mar 2020
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...

Connectez-vous pour commenter.

Plus de réponses (4)

Nicholas Mati
Nicholas Mati le 28 Fév 2017
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 commentaires
Yair Altman
Yair Altman le 29 Juin 2018
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...

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 24 Juin 2011
As of 2010a this was not possible at the user level. I have not heard any evidence that the situation has changed since.

AMM
AMM le 5 Mai 2020
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.

Tracy Fulghum
Tracy Fulghum le 10 Mar 2023
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.

Community Treasure Hunt

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

Start Hunting!

Translated by