1
+ function obj = getStateflowObj(node )
2
+ % GETSTATEFLOWOBJ Get the model element corresponding to the Stateflow
3
+ % node. Stateflow objects don't have handles like Simulink, therefore this
4
+ % function returns the object itself. Note: The model needs to be open.
5
+ %
6
+ % Inputs:
7
+ % node xmlcomp.Node object.
8
+ %
9
+ % Outputs:
10
+ % obj Stateflow object.
11
+
12
+ obj = ' ' ;
13
+ chart_node = getStateflowParent(node );
14
+ chart = find(sfroot , ' -isa' , ' Stateflow.Chart' , ' Name' , chart_node .Name );
15
+
16
+ for i = 1 : length(chart ) % Multiple charts with the same name can be found, so search all
17
+
18
+ if ~isempty(node .Parameters )
19
+ %% Get type from model by following the SSID, name, or label string
20
+ paramNames = {node .Parameters .Name };
21
+ paramVals = {node .Parameters .Value };
22
+ idx_id = find(strcmpi(paramNames , ' ID' ));
23
+ idx_ssid = find(strcmpi(paramNames , ' SSID' ));
24
+ idx_lbl = find(strcmpi(paramNames , ' labelString' ));
25
+ idx_name = find(strcmpi(paramNames , ' Name' ));
26
+ idx_text = find(strcmp(paramNames , ' Text' ));
27
+ idx_port = find(strcmp(paramNames , ' Port' ));
28
+ idx_block = find(strcmp(paramNames , ' BlockType' ));
29
+
30
+ if numel(idx_name ) > 1 % If both 'name' and 'Name' found, use 'Name'
31
+ idx_name = find(strcmp(paramNames , ' Name' ));
32
+ end
33
+
34
+ obj = ' ' ;
35
+ if node == chart_node
36
+ % Node is the chart itself
37
+ obj = chart(i );
38
+ elseif ~isempty(idx_id )
39
+ % Try to find and object with the same SSID
40
+ obj = find(chart(i ), ' ssid' , str2num(paramVals{idx_id }));
41
+ elseif ~isempty(idx_ssid )
42
+ obj = find(chart(i ), ' ssid' , str2num(paramVals{idx_ssid }));
43
+ elseif ~isempty(idx_lbl )
44
+ obj = find(chart(i ), ' LabelString' , paramVals{idx_lbl });
45
+ elseif ~isempty(idx_name ) && (isempty(idx_port ) && isempty(idx_block ))
46
+ obj = find(chart(i ), ' -isa' , ' Stateflow.Data' , ' Name' , paramVals{idx_name });
47
+ elseif ~isempty(idx_name ) && ~isempty(idx_port )
48
+ obj = find(chart(i ), ' -isa' , ' Stateflow.Data' , ' Name' , paramVals{idx_name });
49
+ elseif ~isempty(idx_name )
50
+ name = paramVals{idx_name };
51
+ obj = find(chart(i ), ' Name' , name );
52
+ if isempty(obj )
53
+ % Inports/outports may have () appended
54
+ name_stripped = regexprep(name , ' \(.*?\)' , ' ' );
55
+ obj = find(chart(i ), ' Name' , name_stripped );
56
+ end
57
+ if isempty(obj )
58
+ % May be a function
59
+ obj = find(chart(i ), ' LabelString' , name );
60
+ end
61
+ elseif ~isempty(idx_text )
62
+ % Check if annotation by comparing text
63
+ objs = find(chart(i ), ' -isa' , ' Stateflow.Annotation' );
64
+ for j = 1 : length(objs )
65
+ if contains(objs(j ).Text, paramVals{idx_text })
66
+ obj = objs(j );
67
+ break ;
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ if isempty(obj )
74
+ %% Get object from the node name only
75
+ if isempty(node .Name )
76
+ % Check if name matches name
77
+ obj = find(chart(i ), ' -not' , ' -isa' , ' Stateflow.Chart' , ' Name' , node .Name );
78
+
79
+ % Check if label matches name
80
+ if isempty(obj )
81
+ obj = find(chart(i ), ' LabelString' , node .Name );
82
+ end
83
+ end
84
+ end
85
+
86
+ if ~isempty(obj )
87
+ break ;
88
+ end
89
+ end
90
+ end
0 commit comments