Autocomplete of filenames for function input params

22 visualizaciones (últimos 30 días)
Miro
Miro el 8 de Jun. de 2011
Editada: Brian el 12 de Sept. de 2025
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 comentario
Luke Winslow
Luke Winslow el 24 de Jun. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Nicholas Mati
Nicholas Mati el 28 de Feb. de 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 comentarios
Yair Altman
Yair Altman el 29 de Jun. de 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...

Iniciar sesión para comentar.

Más respuestas (4)

Jan
Jan el 24 de Jun. de 2011
Editada: Jan el 17 de Jul. de 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 comentarios
Tyvaughn Holness
Tyvaughn Holness el 9 de Mzo. de 2020
Worked on 2015a! The .json method did not
Yair Altman
Yair Altman el 11 de Mzo. de 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...

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 24 de Jun. de 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 el 5 de Mayo de 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 el 10 de Mzo. de 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.
  2 comentarios
Matt J
Matt J el 19 de Feb. de 2025
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>')
Brian
Brian el 12 de Sept. de 2025
Editada: Brian el 12 de Sept. de 2025
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.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by