matlab的数组、​cell数组等容器可​以声明其内部元素的类​型么?

2 次查看(过去 30 天)
张力翰 刘
张力翰 刘 2023-11-29
比如我有一个类:
myClass
我创建了一个
a=cell(1,100);
并且每个a中都初始化一个myClass的实例
但是我在编写的时候,编辑器无法将a{1,20}视作myClass类,在用.运算符时,无法代码补全类方法,只有在运行期间,编辑器才会将a{1,20}视作myClass,并提供代码补全。
那么是否可以提前提示编辑器a内部元素的类型?

回答(1 个)

Abhinaya Kennedy
Abhinaya Kennedy 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!

类别

Help CenterFile Exchange 中查找有关 MATLAB 快速入门 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!