combineInputSignals.m 2.53 KB
Newer Older
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
function u = combineInputSignals(myStateSpace, t, varargin)
%MISC.SS.COMBINEINPUTSIGNALS reads the input varargin and searches for name-value-pairs that
%coincide with one of myStateSpace.InputName. If a pair is found, that signal is used
%as elements of u. For inputs that are not found, u-elements are set to zero.
%
% Inputs:
%	myStateSpace	state space for which an input signal is designed
%	t				time for which u shall be defined
%	varargin		name-value-pairs containing input signals with names coinciding
%					with myStateSpace.InputName
% Outputs:
%	u				quantity vector of input signals suiting to myStateSpace for the
%					time t.
%
% Example:
% -------------------------------------------------------------------------
% 	myStateSpace = ss(-1, [1, 2], 1, [], 'InputName', {'control', 'disturbance'});
%	t = linspace(0, 4, 201);
%	disturbanceSignal = quantity.Symbolic(sin(sym('t')), 'grid', t, ...
%		'variable', sym('t'), 'name', 'disturbance');
%	u = misc.ss.combineInputSignals(myStateSpace, t, 'disturbance', disturbanceSignal);
%	y = quantity.Discrete(lsim(myStateSpace, u.on(), t), 'grid', t, ...
%		'gridName', 't', 'name', 'y');
%	plot([y; u]);
% -------------------------------------------------------------------------

% read name of input names from myStateSpace and calculate input lengths
myInputName = misc.ss.removeEnumeration(myStateSpace.InputName);
myInputNameUnique = unique(myInputName, 'stable');
30
myInputNameUniqueValidFieldName = strrep(myInputNameUnique, '.', 'POINT');
31
32
33
34
35
36
37
38
myInputLength = zeros(size(myInputNameUnique));
for it = 1 : numel(myInputLength)
	myInputLength(it) = sum(strcmp(myInputNameUnique{it}, myInputName));
end

% search for input signals in varargin or use default value = 0 instead
myParser = misc.Parser();
for it = 1 : numel(myInputNameUnique)
39
	myParser.addParameter(myInputNameUniqueValidFieldName{it}, ...
40
		quantity.Discrete(zeros(numel(t), myInputLength(it)), ...
Jakob Gabriel's avatar
Jakob Gabriel committed
41
			quantity.Domain("t", t), 'name', myInputNameUnique{it}), ...
42
43
		@(v) isa(v, 'quantity.Discrete') & isvector(v) & (numel(v)==myInputLength(it)));
end
44
45
46
47
48
49
50
51
52

% replace '.' in varargin names, to obtain valid fieldnames
newVarargin = varargin;
for it = 1 : numel(newVarargin)
	if ischar(newVarargin{it}) || isstring(newVarargin{it})
		newVarargin{it} = strrep(newVarargin{it}, '.', 'POINT');
	end
end
myParser.parse(newVarargin{:});
53
54

% finally, create resulting input vector
55
u = myParser.Results.(myInputNameUniqueValidFieldName{1});
56
for it = 2 : numel(myInputNameUnique)
57
	u = [u; myParser.Results.(myInputNameUniqueValidFieldName{it})];
58
59
60
end % for

end % combineInputSignals()