setSignalName.m 2.65 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
assert(isa(myStateSpace, "ss"), "1st input must be state space model");
26
assert(iscell(signalNames) || isstring(signalNames), "signalNames must be a string array or cell array");
27
assert(iscell(signalLength) || isvector(signalLength), "signalLength must be a cell array or a vector");
28
29
30
if iscell(signalLength)
	signalLength = cell2mat(signalLength);
end
31
32
33
34
35
36
37
if isstring(signalNames)
	oldSignalNames = signalNames;
	signalNames = cell(size(oldSignalNames));
	for it = 1 : numel(oldSignalNames)
		signalNames{it} = char(oldSignalNames(it));
	end
end
38

39
40
41
42
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");
43
44
45
else
	error("signalType must be 'input' or 'output'");
end
46
assert(numel(signalNames) == numel(signalLength), "signalNames must have same number as signalLength");
47
48
49
50
51
52
53

% 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
54
			myNewSignalNames{myCurrentSignal} = signalNames{it};
55
		else
56
			myNewSignalNames{myCurrentSignal} = [signalNames{it}, '(', num2str(jt), ')'];
57
58
59
60
61
62
		end
		myCurrentSignal = myCurrentSignal + 1;
	end
end

% set property
63
if strcmp(signalType, "input")
64
65
66
67
68
69
	myStateSpace.InputName = myNewSignalNames;
else
	myStateSpace.OutputName = myNewSignalNames;
end
end