Specify type of variable to allow tab completion

조회 수: 24 (최근 30일)
Adam Cooman
Adam Cooman 2019년 8월 5일
답변: Adam Cooman 2019년 10월 30일
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.
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 8월 5일
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?
Adam Cooman
Adam Cooman 2019년 8월 5일
Maybe I did not clearly word my question.
Assume that I have a class:
classdef MyClass
properties
property1
property2
property3
end
end
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')
end
input.property1 = 5
end
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;
end
Matlab knows the type of the "obj" input variable, so it proposes the different properties and methods when typing "obj. + tab"

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Cooman
Adam Cooman 2019년 10월 30일
Ken Atwell replied the question:
The new function argument validation allows to specify the type of function input variables. This will allow tab completion when writing the code.
This is new in Matlab 2019b and will cause syntax errors in any matlab version before 2019b.
More info here:

추가 답변 (1개)

Chidvi Modala
Chidvi Modala 2019년 8월 8일
  댓글 수: 4
Adam Cooman
Adam Cooman 2019년 8월 8일
@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")
Ken Atwell
Ken Atwell 2019년 10월 28일
@Adam: Exactly what you describe was added in R2019b -- see function argument validataion. Tab completion will use this information to try to make more targetted suggestions and completion.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by