MATLAB代码仓库(个人用)

个人用Matlab代码仓库

类的示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% A test for class

%% Note

% ## Why use classes instead of functions
% ### Encapsulation
% ### Inheritance
% ### Polymorphism
% ### Abstraction
% etc...

% ## rule of class
% 1 file can only contains 1 class definition
% Name of class should be the same as file name
% Only comments can exist before the class declaration

% ## < handle or not
% If class doesn't inherit from handle,
% than we need to use `test = test.get_pure_name();`
% instead of `test.get_pure_name();`

classdef ClassAlpha
% all value of this class
properties
name,
pure_name
end

methods
% Construct function
% notice that the name of the methon is equal to class name
function obj = ClassAlpha()
obj.name = 'Alpha_Romeo';
obj.pure_name = '';
end

% Change the value of this class
function obj = get_pure_name(obj)
obj.pure_name = regexprep(obj.name, '_.*', '');
end
end
end

获取Simulink块的所有属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BlockInfo 用于获取块的所有属性

classdef BlockInfo < handle

% 属性
% block 块的路径
% value_list 一个n*2的cell数组,第一列为属性,第二列为属性的值
% 用get_param获取block的所有属性和它们的值
% value_list_content value_list删除属性值为空的行之后的数组
properties
block
value_list
value_list_content
end
methods
function obj = BlockInfo()
obj.block = '';
obj.value_list = '';
obj.value_list_content = '';
end

%% 获得所有参数并存储于列表中
function get_block_param(obj, block_path)
if ~isempty(block_path)
obj.block = block_path;
params = get_param(block_path, 'ObjectParameters');
fields = fieldnames(params);
obj.value_list = cell(length(fields),2);
for i = 1:length(fields)
param = fields{i};
value = get_param(block_path, param);
obj.value_list{i,1} = param;
obj.value_list{i,2} = value;
end
else
disp('Check Input');
end
end

%% 删除数值为空的选项
% 这段代码的工作原理是这样的:
% cellfun('isempty',cellArray(:,2))会返回一个逻辑数组,其中第二列为空的行为true,其余行为false。
% ~操作符会将这个逻辑数组反转,使得第二列为空的行变为false,其余行变为true。
% 最后,cellArray(~cellfun('isempty',cellArray(:,2)),:)会选择出那些第二列不为空的行。
function reform_value_list(obj)
cellArray = obj.value_list;
cellArray = cellArray(~cellfun('isempty',cellArray(:,2)),:);
obj.value_list_content = cellArray;
end
end

end

% % 假设C是您的原始cell数组
% C = cell(10,1);

% % 创建三个空行
% emptyRows = cell(3,1);

% % 在第5行插入空行
% C = vertcat(C(1:4,:), emptyRows, C(5:end,:));

0%