61 lines
1.7 KiB
Matlab
61 lines
1.7 KiB
Matlab
function S = strpad(obj, S, varargin)
|
|
%STRPAD String rightpadding
|
|
% S = strpad(CA) applies rightpadding of spaces to all elements of the
|
|
% cell array CA so that all have the same length and returns the cell
|
|
% array S.
|
|
%
|
|
% S = strpad(CA, n) sets extra rules for the padding. If n = 0 (default),
|
|
% the longest string determines the length. If n > 0, n will be the
|
|
% length until which padding is added. Note that if a string is longer
|
|
% than n, it will not be truncated. If n < 0, -n is a margin, a number of
|
|
% spaces added to the longest string and that then decides the final
|
|
% length for all elements.
|
|
%
|
|
% S = strpad(CA, n, '-truncate') truncates string if their length
|
|
% exceeds the value of n > 0 (see above).
|
|
|
|
% Handle flags
|
|
[flg varargin] = flagParser(varargin, 'truncate');
|
|
|
|
% Handle input
|
|
p = inputParser();
|
|
p.addRequired('S');
|
|
p.addOptional('n', 0);
|
|
p.parse(S, varargin{:});
|
|
p = p.Results;
|
|
|
|
S = p.S;
|
|
n = p.n;
|
|
|
|
% If S is an array, make it a cell array
|
|
if isnumeric(S)
|
|
S = cellfun(@num2str, num2cell(S), 'UniformOutput', false);
|
|
end
|
|
|
|
% If S is a string, make it a cell array
|
|
if ischar(S)
|
|
S = {S};
|
|
end
|
|
|
|
% Padding method depends on n
|
|
if n > 0
|
|
maxLength = p.n;
|
|
else
|
|
maxLength = max(max(cellfun(@length, S))) - n;
|
|
end
|
|
|
|
% For each element, add padding
|
|
S = cellfun(@(s)([s char(zeros(1, maxLength-numel(s))+32)]), S, 'UniformOutput', false);
|
|
|
|
% Truncation
|
|
if flg.truncate
|
|
S = cellfun(@(s)(s(1:maxLength)), S, 'UniformOutput', false);
|
|
end
|
|
|
|
% Display mode TODO
|
|
if nargout > 0
|
|
return;
|
|
end
|
|
|
|
end
|