Effacer les filtres
Effacer les filtres

Auto complete inputs of method using object property

91 vues (au cours des 30 derniers jours)
Cedric Kotitschke
Cedric Kotitschke le 16 Sep 2024 à 15:10
Commenté : Shivam Gothi le 19 Sep 2024 à 14:18
Hey,
I have a class with a property that I would like to use for auto completion:
classdef TestClass
properties
Names
end
methods
function self = TestClass(names)
arguments
names (1,:) string
end
self.Names = names;
end
function printName(self, name)
mustBeMember(name, self.Names)
disp(name)
end
end
end
So when I call the method 'printName', I would like matlab so suggest the possible options, i.e. the elements of the property 'Names'.
How can I do that?
  1 commentaire
Shivam Gothi
Shivam Gothi le 17 Sep 2024 à 2:41
you can achieve the objecive by JSON-formatted file called functionSignatures.json
The below given link for the documentation explains in detail about it:
Additionally you may refer to the MATLAB answers link:
To validate your "functionSignatures.json" files you can use command "validateFunctionSignaturesJSON". Refer to the below documentation for more help:

Connectez-vous pour commenter.

Réponse acceptée

Shivam Gothi
Shivam Gothi le 19 Sep 2024 à 10:55
Modifié(e) : Shivam Gothi le 19 Sep 2024 à 12:51
It is possible to auto complete the inputs to the member functions of the user defined class. I am suggesting a possible workaround, which I found to be working properly.
Refer to the documentation:
To customize code suggestions and completions for your functions and classes, provide MATLABwith information about your function signatures. Function signatures describe the acceptable syntaxes and allowable data types for a function. MATLAB uses this information to display code suggestions and completions in the Editor, Live Editor, and Command Window. Define this function information in a JSON-formatted file calledfunctionSignatures.json.”
If you are saving your “TestClass.m” definition file at a particular location, say “myfolders” and that path is included in the MATLAB search path, create a folder named “resources” in “myfolders” and make a JSON file in the folder “resources”.
But in this case, you need to hard-code the “Names” property of “TestClass”. This is not a valid solution because every time you define a new object of “TestClass” type, you need to manually change the “Names” in the JSON file.
Therefore, I have created a member function named “json_update that will automatically create a JSON file in a “resources” folder. This function is placed inside the “constructor” of “TestClass”. Therefore, the JSON file is updated automatically with the newly allocated value of “Names” properties. I have attached “TestClass.m” file with this answer. I am also attaching the JSON file generated by the below given code. (Here I am attaching .txt file as .json is not supported here)
NOTE: Place the TestClass.m file on the search path of MATLAB. The "constructor" of "TestClass" will automatically create a "resources" folder on that path. It will also create a file named "functionSignatures.json" inside "resources" folder. You can verify this by locating the newly created "resources" folder in the current MATLAB search path.
I have executed the code in live script and got the following results:
You can see that we are getting autocomplete as expected by you.
Now, if you manually change the “Names” property as shown below figure, you will need to call “json_update( )” function manually to update the JSON as demonstrated below. This is because, the constructor is not called and that is why we need to manually call the function json_update( )
We can conclude that it is working satisfactorily.
Now, try to define second object of TestClass and name it as “B. We can see that the auto-complete is working on second object also, thus meeting the requirements.
To understand the working behind the code, refer to the following resources:
I hope it helps!
  2 commentaires
Cedric Kotitschke
Cedric Kotitschke le 19 Sep 2024 à 13:23
wow annoying that you have to use another file, but thanks!
Shivam Gothi
Shivam Gothi le 19 Sep 2024 à 14:18
According to my understanding, we have to provide MATLAB with information about our function signatures. The only way to do this is by using JSON files. Consequently, it appears that utilizing JSON files is essential and cannot be avoided.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by