Auto complete inputs of method using object property
조회 수: 7 (최근 30일)
이전 댓글 표시
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
Shivam Gothi
2024년 9월 17일
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:
채택된 답변
Shivam Gothi
2024년 9월 19일
편집: Shivam Gothi
2024년 9월 19일
Hello @Cedric Kotitschke
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 called“functionSignatures.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
Shivam Gothi
2024년 9월 19일
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.
추가 답변 (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!