matlab的数组、cell数组等容器可以声明其内部元素的类型么?
조회 수: 2 (최근 30일)
이전 댓글 표시
比如我有一个类:
myClass
我创建了一个
a=cell(1,100);
并且每个a中都初始化一个myClass的实例
但是我在编写的时候,编辑器无法将a{1,20}视作myClass类,在用.运算符时,无法代码补全类方法,只有在运行期间,编辑器才会将a{1,20}视作myClass,并提供代码补全。
那么是否可以提前提示编辑器a内部元素的类型?
댓글 수: 0
답변 (1개)
AKennedy
2023년 12월 5일
Hi 张力翰 刘,
由于我的母语不是粤语,我将尝试用英语回答这个问题。感谢您的理解。
As I understand, you would like to hint in advance about the type of element inside a class. In MATLAB, you can provide type hints using comments to help the editor understand the type of elements inside a cell array. This can improve code completion and provide better support for class methods. Here's how you can do it:
% Assume myClass is the class you've defined
% Create a cell array
a = cell(1, 100);
% Initialize instances of myClass in each cell
for i = 1:100
a{i} = myClass(); % Initialize with an instance of myClass
end
To provide a type hint to the MATLAB editor, you can use a comment to specify the type of elements inside the cell array:
%{
a: cell array containing instances of myClass
a{1}: myClass
%}
a{1}. % After typing the dot, the editor should provide code completion for methods of myClass
By adding this type hint comment, you can help the MATLAB editor understand the type of elements inside the cell array a. This should enable better code completion and method suggestions for the class 'myClass'.
Hope this helps!
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!