changeSignalName.m 5.55 KB
Newer Older
1
function myStateSpace = changeSignalName(myStateSpace, oldName, newName, varargin)
2
% CHANGESSSIGNALNAME sets the property 'OutputName' or 'InputName' of the ss 
3
4
% myStateSpace by replacing oldName with newName. If hereby inputs result in having
% the same name, they are connected, see example.
5
6
7
8
9
10
11
%
% Inputs:
%	myStateSpace	state space whoms OutputName and InputName should be modified
%	oldName			cell array of signal names that should be applied to
%					myStateSpace.OutputName
%	newName			cell array of same size as signal names. Each element defines the
%					length of the signal that is named in signal names
12
13
14
% Optional Inputs (default values):
%	input (true)	logical 
%	output (true) 	
15
16
17
18
19
% Outputs:
%	myStateSpace	the state space with modified parameters
%
% Example:
% -------------------------------------------------------------------------
20
% 	mySimulationModel = ss(1, [1, 2], [1; 2; 3], [], 'InputName', {'d', 'e'});
21
%	mySimulationModel = misc.ss.setSignalName(mySimulationModel, 'output', {'a', 'b'}, {2, 1});
22
%	mySimulationModel = misc.ss.changeSignalName(mySimulationModel, {'a', 'd'}, {'c', 'e'})
23
24
25
% -------------------------------------------------------------------------

% input checks
26
27
if ~isstring(oldName)
	oldName = string(oldName);
28
end
29
30
if ~isstring(newName)
	newName = string(newName);
31
end
32
33
34
35
36
myParser = misc.Parser();
myParser.addRequired('myStateSpace', @(v) isa(v, 'ss'));
myParser.addParameter('input', true, @(v) islogical(v));
myParser.addParameter('output', true, @(v) islogical(v));
myParser.parse(myStateSpace, varargin{:});
37
38
assert(numel(newName) == numel(oldName), 'number of elements of newName and oldName must be equal');

39
40
41
% consider output
if myParser.Results.output
	myStateSpace = changeOutputName(myStateSpace, oldName, newName);
42
43
end

44
% consider input
45
if myParser.Results.input
46
	myStateSpace = changeInputName(myStateSpace, oldName, newName);
47
48
end
end
49

50
51
52
53
54
55
56
57
58
59
function myStateSpace = changeInputName(myStateSpace, oldName, newName)
	% changeInputName replaces the InputName of myStateSpace that are specified by
	% oldName with the names specified in newName. This is done by creating new gain
	% blocks with inputs according to newName and outputs according to oldName. By
	% doing so, inputs can be renamed and interconnected with other inputs that
	% already had that new name.
	uniqueOldInputName = misc.ss.removeEnumeration(myStateSpace.InputName, true);
	oldInputName = misc.ss.removeEnumeration(myStateSpace.InputName, false);
	for it = 1 : numel(oldName)
		% input check:
60
61
		if any(strcmp(oldName(it), uniqueOldInputName))
			assert(~any(strcmp(oldName(it), newName)), ...
62
63
64
65
66
				'overlaps in oldName and newName are not supported for InputName-changes');
		end
	end	
	
	for it = 1 : numel(oldName)
67
68
69
70
71
		if any(strcmp(oldName(it), uniqueOldInputName))
			% there is an InputName with the name oldName(it) to be replaced.
			numberOfThisInput = sum(strcmp(oldInputName, oldName(it)));
			if any(strcmp(newName(it), uniqueOldInputName))
				% there is already a InputName with the name of newName(it)
72
73
74
				% -> they are interconnected via thisConnectorGain
				thisConnectorGain = ss([], [], [], ...
					vertcat(eye(numberOfThisInput), eye(numberOfThisInput)), ...
75
					'InputName', newName(it));
76
				thisConnectorGain = misc.ss.setSignalName(thisConnectorGain, 'output', ...
77
78
					[oldName(it); newName(it) + "tempChangeSignalName"], ...
					[numberOfThisInput; numberOfThisInput]);
79
				myStateSpace = misc.ss.changeSignalName(myStateSpace, ...
80
					newName(it), newName(it) + "tempChangeSignalName", 'output', false);
81
			else
82
83
84
				% there is no InputName with the name of newName(it)
				% -> thisConnectorGain is just an identy with input newName(it) and
				% output oldName(it)
85
86
				thisConnectorGain = ss([], [], [], ...
					vertcat(eye(numberOfThisInput)), ...
87
					'InputName', newName(it), 'OutputName', oldName(it));
88
			end
89
90
			inputNameIntermediate = misc.ss.removeEnumeration(myStateSpace.InputName, false);
			newInputNames = [inputNameIntermediate(...
91
92
93
				~strcmp(inputNameIntermediate, oldName(it)) & ...
				~strcmp(inputNameIntermediate, newName(it) + "tempChangeSignalName"))];
			if ~any(strcmp(newInputNames, newName(it)))
94
				newInputNames = [newInputNames; ...
95
					repmat(newName(it), sum(strcmp(oldInputName, oldName(it))), 1)];
96
			end
97
98
99
100
101
			myStateSpace = misc.ss.connect(...
				newInputNames, myStateSpace.OutputName, ...
				myStateSpace, thisConnectorGain);
		end
	end
102

103
104
105
106
107
108
109
end
function myStateSpace = changeOutputName(myStateSpace, oldName, newName)
	% changeOutputName replaces the OutputName of myStateSpace that are specified by
	% oldName with the names specified in newName. This is done by reading the old
	% OutputName property, replacing the strings, and setting them in the end again.
	uniqueOldInputName = misc.ss.removeEnumeration(myStateSpace.OutputName, true);
	for it = 1 : numel(newName)
110
111
		assert(~any(strcmp(uniqueOldInputName, newName(it))), ...
			"newName is already OutputName of myStateSpace - not implemented");
112
113
	end
	% split signal names
114
115
	oldOutputNames = string(misc.ss.removeEnumeration(myStateSpace.OutputName));
	%newOutputNames = misc.ss.removeEnumeration(myStateSpace.OutputName);
116
117
118
	
	% Exchange names of newOutputNames cell array:
	for it = 1 : numel(newName)
119
120
121
		if ~isempty(strcmp(oldOutputNames, oldName(it))) ...
				&& any(strcmp(oldOutputNames, oldName(it)))
			[oldOutputNames(strcmp(oldOutputNames, oldName(it)))] = deal(newName(it));
122
123
		end
	end
124
125
	uniqueOutputNames = unique(oldOutputNames, "stable");
	outputLength = arrayfun(@(v) sum(strcmp(oldOutputNames, v)), uniqueOutputNames);
126
	% set output names
127
	myStateSpace = misc.ss.setSignalName(myStateSpace, "output", uniqueOutputNames, outputLength);
128
end