setSignalName.m 2.43 KB
Newer Older
1
function myStateSpace = setSignalName(myStateSpace, signalType, signalNames, signalLength)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
% SETSSSIGNALNAME sets the property 'OutputName' or 'InputName' of the ss 
% myStateSpace to signalNames. Therein, signalLength how many output elements belong 
% to a signalName. Wheather the InputName or the OutputName properties should be set
% is determined by signalType \in {'input', 'output'}
%
% Inputs:
%	myStateSpace	state space whoms OutputName should be modified
%	signalType		must be either 'input' or 'output' and determines of OutputName
%					or InputName are set
%	signalNames		cell array of signal names that should be applied to
%					myStateSpace.OutputName
%	signalLength	cell array of same size as signal names. Each element defines the
%					length of the signal that is named in signal names
% Outputs:
%	myStateSpace	the state space with modified parameters
%
% Example:
% -------------------------------------------------------------------------
% 	mySimulationModel = ss(1, 1, [1; 2; 3], []);
21
%	mySimulationModel = misc.ss.setSignalName(mySimulationModel, 'output', {'a', 'b'}, {2, 1});
22
23
24
% -------------------------------------------------------------------------

% input checks
25
26
27
assert(isa(myStateSpace, "ss"), "1st input must be state space model");
assert(iscell(signalNames), "signalNames must be a cell array");
assert(iscell(signalLength) || isvector(signalLength), "signalLength must be a cell array or a vector");
28
29
30
31
if iscell(signalLength)
	signalLength = cell2mat(signalLength);
end

32
33
34
35
if strcmp(signalType, "input")
	assert(size(myStateSpace, 2) == sum([signalLength(:)]), "signalLength does not fit to state space");
elseif strcmp(signalType, "output")
	assert(size(myStateSpace, 1) == sum([signalLength(:)]), "signalLength does not fit to state space");
36
37
38
else
	error("signalType must be 'input' or 'output'");
end
39
assert(numel(signalNames) == numel(signalLength), "signalNames must have same number as signalLength");
40
41
42
43
44
45
46

% create name cell array
myNewSignalNames = cell(sum([signalLength(:)]), 1);
myCurrentSignal = 1;
for it = 1 : numel(signalNames)
	for jt = 1 : signalLength(it)
		if signalLength(it) == 1
47
			myNewSignalNames{myCurrentSignal} = char(signalNames{it});
48
		else
49
			myNewSignalNames{myCurrentSignal} = [char(signalNames{it}), '(', num2str(jt), ')'];
50
51
52
53
54
55
		end
		myCurrentSignal = myCurrentSignal + 1;
	end
end

% set property
56
if strcmp(signalType, "input")
57
58
59
60
61
62
	myStateSpace.InputName = myNewSignalNames;
else
	myStateSpace.OutputName = myNewSignalNames;
end
end