autocomplete filenames paths function input

10 views (last 30 days)
I am trying to get custom function input autocompletion for a filename and can't get it to work how I want:
  • I have a function like "dir", so I want it to have autocompletion for the target folder as "dir" does, but I also want to be able to pass optional arguments to extend its functionality
  • I want to use the arguments block as much as possible, and functionSignatures.json if needed
The function itself would be something like
function mydir(target,opts)
arguments
target
opts.ShowHidden
opts.Blahblah
end
% do stuff
end
Some things that don't work:
  • I tried to make a functionSignature for the "mydir" function itself, but only for the "target" variable. Then I do get the tab-autocomplete behavior I want, and wildcards work. However, I don't get tab-autocomplete on my optional arguments. I could not figure out how to do the structure-based name/value pair function signatures, as that does not seem to be covered in the documentation.
  • Within the arguments block, I noticed that using the "mustBeFolder" validator makes it possible to tab-autocomplete the input target. However, I cannot then include wildcards because once wildcards are in, then the folder doesn't exist anymore.
  • So I tried to create a custom "mustBeValidPathName" validator and created a functionSignatures.json entry for it, but then the tab-autocomplete doesn't work anymore. Calling "mustBeValidPathName" by itself, however, does give me the tab-autocomplete behavior I want.
How can I get both the file name autocompletion, that accepts wildcards, and be able to also auto-complete optional arguments using the arguments block framework?
There are 2 other Matlab Answers pages on the topic but first one doesn't seem to answer the question, and second one is reference by the first but is outdated/incomplete
  1. https://www.mathworks.com/matlabcentral/answers/2030399-tab-autocompletions-in-functions-for-paths?s_tid=ta_ans_results
  2. https://in.mathworks.com/matlabcentral/answers/9046-autocomplete-of-filenames-for-function-input-params

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 12 Jun 2025
Edited: Benjamin Kraus on 12 Jun 2025
I don't think it is possible to do what you are requesting using an arguments block alone without also using functionSignatures.json. I'm also pretty sure that any use of functionSignatures.json will replace auto-complete from the arguments block. I don't think the two will "merge".
I don't think creating functionSignatures.json for your custom validator will do what you were hoping, but that is an interesting enhancement. I will forward that feedback to the relevant team.
It sounds like you were able to get functionSignatures.json working for your target input, so all that is lacking is adding entries for your two name/value pairs (ShowHidden and Blahblah).
I believe what you are looking for is the "namevalue" kind of input, which is mentiond on the "Customize Code Suggestions and Completions" documentation page.
The JSON would look something like this (this is largely copied from the documation page above):
{
"_schemaVersion": "1.0.0",
"mydir":
{
"inputs":
[
{"name":"target", "kind":"ordered", "type":["char"]},
{"name":"ShowHidden", "kind":"namevalue", "type":["logical","scalar"]},
{"name":"Blahblah", "kind":"namevalue", "type":["char", "choices={'Default','Choice1','Choice2'}"]}
]
}
}
  1 Comment
J. Alex Lee
J. Alex Lee on 12 Jun 2025
Thanks for the explanation that functionSignatures.json will replace the arguments auto-complete without merging.
Yes, the "kind":"namevalue" does seem to work in my case - this wasn't immediately obvious since the signature in the function definition uses a struct scalar rather than a varargin / cellarray, but in retrospect makes sense.
The following signature works with the above arguments block, and also gives the auto-complete behavior expected. Thanks!
{
"mydir":
{
"inputs":
[
{"name":"target", "kind":"required", "type":[["file"],["folder"]]},
{"name":"ShowHidden", "kind":"namevalue", "type":["logical","scalar"]},
{"name":"Blahblah", "kind":"namevalue", "type":["logical","scalar"]}
]
}
}
Thanks also for forwarding the use-case, but I feel this is quite an edge case...I think it would be more useful for me to understand if there's any in-language equivalent to the "type":[["file"],["folder"]] part of the json signature, and how to leverage that.
Anyway, thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Install Products in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!