time | calls | line |
---|
| | 1 | function t = strjust(s,justify)
|
| | 2 | %STRJUST Justify character array.
|
| | 3 | % T = STRJUST(S) or T = STRJUST(S,'right') returns a right justified
|
| | 4 | % version of the character array S.
|
| | 5 | %
|
| | 6 | % T = STRJUST(S,'left') returns a left justified version of S.
|
| | 7 | %
|
| | 8 | % T = STRJUST(S,'center') returns a center justified version of S.
|
| | 9 | %
|
| | 10 | % See also DEBLANK, STRTRIM.
|
| | 11 |
|
| | 12 | % Copyright 1984-2006 The MathWorks, Inc.
|
| | 13 |
|
| 120 | 14 | if nargin<2
|
| 120 | 15 | justify = 'right';
|
| | 16 | else
|
| | 17 | justify = lower(justify);
|
| | 18 | end
|
| | 19 |
|
| 120 | 20 | if isempty(s)
|
| | 21 | t = s;
|
| | 22 | return;
|
| | 23 | end
|
| | 24 |
|
| 120 | 25 | [m,n] = size(s);
|
| | 26 |
|
| 120 | 27 | spaceCol = 0;
|
| 120 | 28 | switch justify
|
| 120 | 29 | case 'right'
|
| 120 | 30 | spaceCol = n;
|
| | 31 | case 'left'
|
| | 32 | spaceCol = 1;
|
| | 33 | end
|
| | 34 |
|
| 120 | 35 | if spaceCol
|
| 120 | 36 | if ~any(isspace(s(:,spaceCol)))
|
| 120 | 37 | t = s;
|
| 120 | 38 | return;
|
| | 39 | end
|
| | 40 | end
|
| | 41 |
|
| | 42 | % Find non-pad characters
|
| | 43 | ch = (s ~= ' ' & s ~= 0);
|
| | 44 | [r,c] = find(ch);
|
| | 45 |
|
| | 46 | % Determine offset
|
| | 47 | switch justify
|
| | 48 | case 'right'
|
| | 49 | [dum,offset] = max(fliplr(ch),[],2);
|
| | 50 | offset = offset - 1;
|
| | 51 | case 'left'
|
| | 52 | [dum,offset] = max(ch,[],2);
|
| | 53 | offset = 1 - offset;
|
| | 54 | case 'center'
|
| | 55 | [dum,offsetR] = max(fliplr(ch),[],2);
|
| | 56 | [dum,offsetL] = max(ch,[],2);
|
| | 57 | offset = floor((offsetR - offsetL)/2);
|
| | 58 | otherwise
|
| | 59 | error(message('MATLAB:strjust:UnknownParameter'));
|
| | 60 | end
|
| | 61 |
|
| | 62 | % Apply offset to justify character array
|
| | 63 | newc = c + offset(r);
|
| | 64 | t = repmat(' ',m,n);
|
| | 65 | t(r + (newc-1)*m) = s(r + (c-1)*m);
|