MATLAB Answers


Specify type of variable to allow tab completion

Asked by Adam Cooman on 5 Aug 2019
Latest activity Commented on by Adam Cooman on 8 Aug 2019
Is there a way to indicate the type of a variable to the static analysis tool in Matlab to allow tab completion when coding?
This could be usefull when passing objects as an argument to a function.


Would this be for the purpose of text to display in a pop-up hint? Or is the idea that the tab completion would examine all of the variables that are in scope and only list for completion those that have particular data types?
Maybe I did not clearly word my question.
Assume that I have a class:
classdef MyClass
Now, I have a function which takes an object of this class as one of its inputs:
function MyFunction(input)
if ~isa(input,'MyClass')
error('wrong input type')
input.property1 = 5
Because the type of the "input" variable is unknown to the static analysis tool, I cannot use tab completion on the "input" variable. I would find it very usefull if tab completion would propose me a list of the different properties and functions that are available on "MyClass".
This works already when programming a dynamic method on a class.
When, for example, I add this simple function to "MyClass".
function res=DynamicMethod(obj)
obj.property1 = 5;
Matlab knows the type of the "obj" input variable, so it proposes the different properties and methods when typing "obj. + tab"

Sign in to comment.

1 Answer

Answer by Chidvi Modala on 8 Aug 2019


This only works while constructing the call to a function, to give hints as to what the proper arguments are for that function. This does not work while creating code for the function itself -- it does not give hints as to what valid class methods or class property names are while typing in the code that implements the class.
For that, the easiest is to construct a dummy object at the command prompt with the same name as the variable in the function.
So, at the command prompt create:
input = MyClass; %+ whatever arguments the constructor expects.
Then, you'll get tab completion when writing the function code (even though input shouldn't be in scope).
Bad variable name by the way, input is a matlab function. Plus it doesn't tell you anything about what's in the variable (other than it's the input to the function which the reader already knows).
@Chivdi and Walter: Indeed, I'm looking for tab completion during coding of the function itself, not for when I want to call the function afterwards.
I think it could be a usefull feature to use a syntax similar to property validation to function inputs. I noticed that tab completion works when I use a property which has a specified type in its validation.
@Guillaume: Thanks for the tip, creating a dummy object is indeed a solution, but a little bit of a hassle. (my bad variable name was just for this dummy example, just like the great property names "property1" "property2" and "property3")

Sign in to comment.