Skip to content

Commit

Permalink
strjust function + tf display centered
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Oct 15, 2023
1 parent 4e8ab3b commit e94e414
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `cell2mat` Convert cell array of matrices to single matrix.
- [#948](http://github.com/Nelson-numerical-software/nelson/issues/948) `blkdiag` Create a block diagonal matrix from 2D matrices of different sizes.
- `kron` Kronecker tensor product.
- `strjust` Justify strings.

- control system module (part 1):
- [#967](http://github.com/Nelson-numerical-software/nelson/issues/967) control system module template.
Expand Down
52 changes: 42 additions & 10 deletions modules/control_system/functions/@tf/display.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,22 @@ function display(varargin)
numeratorString = stringPoly(numerators{k, q}, TF.Variable);
haveNoNumerator = isempty(numeratorString);
denominatorString = stringPoly(denominator{k, q}, TF.Variable);
lengthDiff = length(denominatorString) - length(numeratorString);
if (lengthDiff > 0)
spacesToAdd = repmat(' ', 1, floor(lengthDiff / 2));
numeratorString = [spacesToAdd, numeratorString];
end
isOneString = strcmp(denominatorString, ' 1');

maxLen = max([length(denominatorString), length(numeratorString)]);
spacesToAdd = repmat(' ', 1, maxLen - length(numeratorString));
numeratorString = [spacesToAdd, numeratorString];
spacesToAdd = repmat(' ', 1, maxLen - length(denominatorString));
denominatorString = [spacesToAdd, denominatorString];
ce = strjust({numeratorString, denominatorString}, 'center');
numeratorString = ce{1};
denominatorString = ce{2};
numeratorString = deblank(numeratorString);
denominatorString = deblank(denominatorString);
dashString = getDashedLine(numeratorString, denominatorString);

if (m ~= 1)
if (strcmp(denominatorString, ' 1') == true)
if isOneString
if ~haveNoNumerator
disp([' ',sprintf(_('%d:'), k), ' ', numeratorString]);
else
Expand All @@ -74,8 +81,7 @@ function display(varargin)
end
end


if (strcmp(denominatorString, ' 1') == false && ~haveNoNumerator)
if (~isOneString && ~haveNoNumerator)
disp([' ', dashString]);
disp([' ', denominatorString]);
end
Expand All @@ -101,9 +107,35 @@ function display(varargin)
end
end
%=============================================================================
function [numeratorString, dashString, denominatorString] = uniformizeStrings(numeratorString, dashString, denominatorString)

str = string({numeratorString, dashString, denominatorString});
max_length = max(strlength(str));

spaces_numeratorString = (max_length - length(numeratorString));
spaces_dashString = (max_length - length(dashString));
spaces_denominatorString = (max_length - length(denominatorString));

text = strjust({dashString, denominatorString}, 'center');
dashString = text{1};
denominatorString = text{2};

str{1} = [repmat(' ', 1, spaces_numeratorString), str{1}];
str{2} = [repmat(' ', 1, spaces_dashString), str{2}];
str{3} = [repmat(' ', 1, spaces_denominatorString), str{3}];

str = strjust(str, 'center');
numeratorString = deblank(str{1});
dashString = deblank(str{2});
denominatorString = deblank(str{3});
end
%=============================================================================
function dash = getDashedLine(numeratorString, denominatorString)
dash = '';
lenDash = max(length(numeratorString), length(denominatorString)) + 1;
lenDash = max(length(numeratorString), length(denominatorString));
if ~mod(lenDash, 2)
lenDash = lenDash + 1;
end
for i = 1:lenDash
dash = strcat(dash, '');
end
Expand Down
12 changes: 10 additions & 2 deletions modules/control_system/functions/@tf/subsasgn.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@
%=============================================================================
function objOut = updateNumerator(objIn, value)
st = struct(objIn);
st.Numerator = value;
if ~iscell(value)
st.Numerator = {value};
else
st.Numerator = value;
end
objOut = class(st, 'tf');
end
%=============================================================================
function objOut = updateDenominator(objIn, value)
st = struct(objIn);
st.Denominator = value;
if ~iscell(value)
st.Denominator = {value};
else
st.Denominator = value;
end
objOut = class(st, 'tf');
end
%=============================================================================
Expand Down
84 changes: 42 additions & 42 deletions modules/control_system/tests/test_tf.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
REF = '
sys =
2 s
—————————————————
4 s^3 + 3 s - 1
2 s
———————————————
4 s^3 + 3 s - 1
Continuous-time transfer function.
Expand All @@ -31,9 +31,9 @@
REF = '
sys =
2 z
—————————————————
4 z^3 + 3 z - 1
2 z
———————————————
4 z^3 + 3 z - 1
Sample time: 1.5000 seconds
Discrete-time transfer function.
Expand All @@ -49,14 +49,14 @@
sys =
From input 1 to output:
2 s
—————————————————
4 s^3 + 3 s - 1
2 s
———————————————
4 s^3 + 3 s - 1
From input 2 to output:
s + 3
———————————————
s^2 + 3 s + 5
s + 3
—————————————
s^2 + 3 s + 5
Continuous-time transfer function.
Expand All @@ -78,32 +78,32 @@
sys =
From input 1 to output:
1: 1
1: 1
2: 5
2: 5
3: 8
3: 8
From input 2 to output:
1: 2
1: 2
2: 6
2: 6
3: 9
3: 9
From input 3 to output:
1: 3
1: 3
2: 7
2: 7
3: 10
3: 10
From input 4 to output:
1: 4
1: 4
2: 8
2: 8
3: 11
3: 11
Static gain.
Expand All @@ -116,11 +116,11 @@
sys = tf(num, den, Ts);
sys.Variable = 'z^-1';
R = evalc('display(sys)');
REF = '
REF = '
sys =
3 z^-1 + 4 z^-2
—————————————————
—————————————————
3 + z^-1 + 5 z^-2
Sample time: 0.2000 seconds
Expand All @@ -138,14 +138,14 @@
REF = '
sys =
- 1 + 3 z^-1 - 4 z^-2
———————————————————————
- 1 + 5 z^-1 - 7 z^-2
- 1 + 3 z^-1 - 4 z^-2
—————————————————————
- 1 + 5 z^-1 - 7 z^-2
Sample time: 0.2000 seconds
Discrete-time transfer function.
';
' ;
assert_isequal(R, REF);
%=============================================================================
sys1 = tf([1 -1],[1 1]);
Expand All @@ -156,14 +156,14 @@
sys =
1:
s - 1
———————
s + 1
s - 1
———————
s + 1
2:
s + 2
———————————————
s^2 + 4 s + 5
s + 2
—————————————
s^2 + 4 s + 5
Continuous-time transfer function.
Expand All @@ -178,14 +178,14 @@
sys =
From input 1 to output:
s - 1
———————
s + 1
s - 1
———————
s + 1
From input 2 to output:
s + 2
———————————————
s^2 + 4 s + 5
s + 2
—————————————
s^2 + 4 s + 5
Continuous-time transfer function.
Expand Down
35 changes: 35 additions & 0 deletions modules/string/functions/@cell/strjust.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%=============================================================================
% Copyright (c) 2016-present Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
function varargout = strjust(varargin)
narginchk(1, 2);
nargoutchk(0, 1);

if nargin == 1
justify = 'right';
else
justify = lower(varargin{2});
end
mustBeMember(justify, ["left", "right", "center"], 2);

str = varargin{1};
if ~iscellstr(str)
error(_('String, cell of chars or characters vector expected.'));
end
result = str;
for k = 1:numel(str)
if ischar(str{k})
result{k} = strjust(str{k}, justify);
else
error(_('String, cell of chars or characters vector expected.'));
end
end
varargout{1} = result;
end
%=============================================================================
32 changes: 32 additions & 0 deletions modules/string/functions/@string/strjust.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%=============================================================================
% Copyright (c) 2016-present Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
function varargout = strjust(varargin)
narginchk(1, 2);
nargoutchk(0, 1);

if nargin == 1
justify = 'right';
else
justify = lower(varargin{2});
end
mustBeMember(justify, ["left", "right", "center"], 2);

str = varargin{1};
result = str;
for k = 1:numel(str)
if ischar(str{k})
result{k} = strjust(str{k}, justify);
else
error(_('String, cell of chars or characters vector expected.'));
end
end
varargout{1} = result;
end
%=============================================================================
Loading

0 comments on commit e94e414

Please sign in to comment.