Autocomplete of filenames for function input params
조회 수: 58 (최근 30일)
이전 댓글 표시
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
Luke Winslow
2011년 6월 24일
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.
채택된 답변
Jan
2011년 6월 24일
편집: Jan
2017년 7월 17일
There you find instructions for editing:
edit(fullfile(matlabroot,'toolbox/local/TC.xml'))
댓글 수: 4
Yair Altman
2020년 3월 11일
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...
추가 답변 (4개)
Nicholas Mati
2017년 2월 28일
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
Walter Roberson
2018년 6월 28일
Official documentation of this is at https://www.mathworks.com/help/matlab/matlab_prog/customize-code-suggestions-and-completions.html
Yair Altman
2018년 6월 29일
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...
Walter Roberson
2011년 6월 24일
As of 2010a this was not possible at the user level. I have not heard any evidence that the situation has changed since.
댓글 수: 0
AMM
2020년 5월 5일
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.
댓글 수: 0
Tracy Fulghum
2023년 3월 10일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 JSON Format에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!