Clear Filters
Clear Filters

Advanced matlab function autocompletion based on previous input

31 views (last 30 days)
Hello Matlab community! first question for me:
Short question: is there an easy/documented way (or is it gonna be implemented in future releases) of providing input autocompletion for functions based on previous inputs?
Context: I found very useful the link below provided in other question/answer about autocompletion but it seems to me it has been somehow abandoned...
the usage of mutuallyExclusiveGroup – Definition of set of exclusive arguments goes next by the desired result but not close enough.
To make a practical example my ideal result would be to get to the same user experience level that the function readcell(...) offers: if I provide an xls filename as first input and then type "sheet" matlab knows automatically the sheetnames of that file and suggest them. I tried to navigate inside the readcell(..) code but I think I'm missing something..
The autocompletion approach with the json template is kinda 'static' in my opinion, since I don't know what file the user will enter, I can't enumerate all the options manually, and I don't know how to filter something more specific than a file-format like *mat, *xlsx,...
Any workaround? Or maybe I'm just not smart enough to make it work.
Another thing I use consistently for the autocompletion is the "argument (Input)" syntax for my purposes, usually I use
{mustBeMember(__,[option1, option2, ..]} and that helps me a lot to provide fast ways to make autocompletions for end users. But also in this situation I see no way to reuse the information of a previous input.. in example it could be cool, let's say, having this:
function bill = iWannaEat( restaurant, dish) % < let's keep things simple and assume this is a simple function, or a static method.
arguments (Input)
restaurant (1,1) KitchenObject
dish (1,:) string {mustBeMember(dish, restaurant.MenuSheet)} = "water" % < but I can't reuse
% restaurant.MenuSheet,
% because a .MenuSheet must be guaranteed to exist?
end
bill = ... things ...
end
By the way I appreciate the extraordinary work you do for free to help other programmers with their things!!!
I went through 2014a till the 2024 and the developments has been huge, do you agree?
clc, clear all

Accepted Answer

Jeremy Huard
Jeremy Huard on 25 Jul 2024
the autocompletion defined in the function signature JSON file does not have to be static.
For example, the function signature of readcell specifies a choice that is generated dynamically from the filename: choices=sheetnames(filename)
The property choices accepts any MATLAB code and can use any of the previously defined inputs so you can make it as dynamic as you wish.
Check the functionSignatures.json file in the resources subfolder located in the same folder as readcell.m .
In that file look for readcell to find its input definition.
I hope this helps.
Jérémy
  2 Comments
Augusto Mazzei
Augusto Mazzei on 28 Jul 2024 at 23:35
Edited: Augusto Mazzei on 28 Jul 2024 at 23:47
Thank you Jeremy for your contribution. First of all thank you for specifying the subfolder resources because I didn't manage to find the function signature json, before opening my question. Secondly, I post a chunk that I consider really inspiring of how readcell autocompletion works.
Please if there is a link in the Matlab documentation that provides an example as good as the one you spotted to me, consider the idea to integrate it in the documentation of the top-search links https://it.mathworks.com/help/mps/restfuljson/matlab-function-signatures-in-json.html
(stress out three topics)
-you can specify any crafted matlab class and handles as 'type'
-multiple 'types' with nested barckets with dedicated restrictions
-reuse matlab functions on previous inputs as choices (as you said choices=sheetnames(filename)!!)
Jeremy, really thank you!!!
"readcell": {
"inputs":[{
"mutuallyExclusiveGroup":[[
(!) cool>>{"name":"filename","kind":"required","type":[["char","file"],["char","@(x)matlab.io.internal.vfs.validators.hasIriPrefix(x)"]]},
{"mutuallyExclusiveGroup":[[
{"name":"opts","kind":"required","type":"matlab.io.text.TextImportOptions"},
{"name":"DateLocale","kind":"namevalue","type":["char"]},
{"name":"Encoding","kind":"namevalue","type":[["char"],["char","choices=matlab.io.internal.shared.EncodingInput.encodingSuggestions()"]]},
{"name":"TextType","kind":"namevalue","type":["char","choices={'char','string'}"]},
{"name":"DatetimeType","kind":"namevalue","type":["char","choices={'text','datetime'}"]},
{"name":"DurationType","kind":"namevalue","type":["char","choices={'duration','text'}"]},
{"name":"WebOptions","kind":"namevalue","type":"weboptions"}
],[
(!) cool>> {"name":"opts","kind":"required","type":"matlab.io.spreadsheet.SpreadsheetImportOptions"},
(!) cool>> {"name":"Sheet","kind":"namevalue","type":[["char","choices=sheetnames(filename)"],["numeric","scalar","integer",">=1"]]},
{"name":"UseExcel","kind":"namevalue","type":["logical","scalar"]},
{"name":"TextType","kind":"namevalue","type":["char","choices={'char','string'}"]},
{"name":"DatetimeType","kind":"namevalue","type":["char","choices={'text','datetime','exceldatenum'}"]},
{"name":"WebOptions","kind":"namevalue","type":"weboptions"}
]]}
],[ ...
Jeremy Huard
Jeremy Huard on 29 Jul 2024 at 12:07
glad it helped!
I agree with you. A dedicated example for dynamic code completions defined in function signatures would be great.
Currently, there is only one tip in the table that defines valid arguments for the type property:
But it could be made more visible. I have shared this request with our dev team.
Thank you for your valuable feedback!
Best,
Jérémy

Sign in to comment.

More Answers (1)

Dinesh
Dinesh on 12 Apr 2024
Hello,
As per my understanding, MATLAB does not natively support dynamic, context-sensitive autocompletion for user-defined functions directly through simple settings or annotations.
As you've found, MATLAB allows for some level of customization of code suggestions and completions through JSON files, but this method is indeed somewhat static. It's designed more for static API like scenarios where the inputs are known beforehand, not for dynamic content that changes based on previous inputs.
  1 Comment
Augusto Mazzei
Augusto Mazzei on 12 Apr 2024
Edited: Augusto Mazzei on 12 Apr 2024
Hello Dinesh, thanks for replying. I'll wait a little more before voting, because as we're agreeing on the fact that there is no easy way with this conventional methods to make code suggestion dynamic, still persist the fact that functions like 'readcell(..)' provide what I'm looking for, and so I wonder: why not replicate it? what's the underlying solution?

Sign in to comment.

Categories

Find more on Just for fun in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!