File: StrPad.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (35 lines) | stat: -rw-r--r-- 989 bytes parent folder | download | duplicates (6)
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
30
31
32
33
34
35
function str = StrPad(in,len,char)
% str = StrPad(in,length,padchar)
% (pre)pads IN with CHAR to sepcified length LEN. If inputs IN or PADCHAR
% are numerical, they will be converted to to string. If input is too long,
% it is truncated from the start to specified length.
%
% DN 2007

if isnumeric(in) && length(in)==1 && in==round(in)
    % convert to string
    in = num2str(in);
end
if isnumeric(char) && length(char)==1
    % convert to string
    char = num2str(char);
end
if ischar(in)
    % check that we have a string
    inlen = length(in);
    if inlen > len
        % truncate
        b = inlen - len; % string is b characters too long
        str = in(b+1:end);
    elseif inlen == len
        % string is right length already
        str = in;
    else
        % pre-pad
        b = len - inlen; % string is b characters too short
        str = [repmat(char,1,b) in];
    end
else
    error('input must be char or scalar integer');
end