comparison scripts/strings/strjust.m @ 11469:c776f063fefe

Overhaul m-script files to use common variable name between code and documentation.
author Rik <octave@nomad.inbox5.com>
date Sun, 09 Jan 2011 12:41:21 -0800
parents 564e998017f5
children fd0a3ac60b0e
comparison
equal deleted inserted replaced
11468:e1edf0ba3bcb 11469:c776f063fefe
38 ## @end group 38 ## @end group
39 ## @end example 39 ## @end example
40 ## @seealso{deblank, strrep, strtrim, untabify} 40 ## @seealso{deblank, strrep, strtrim, untabify}
41 ## @end deftypefn 41 ## @end deftypefn
42 42
43 function y = strjust (x, just) 43 function y = strjust (s, pos)
44 44
45 if (nargin < 1 || nargin > 2) 45 if (nargin < 1 || nargin > 2)
46 print_usage (); 46 print_usage ();
47 endif 47 endif
48 48
49 if (nargin == 1) 49 if (nargin == 1)
50 just = "right"; 50 pos = "right";
51 else 51 else
52 just = tolower (just); 52 pos = tolower (pos);
53 endif 53 endif
54 54
55 if (ndims (x) != 2) 55 if (ndims (s) != 2)
56 error ("strjust: input must be a string or character matrix"); 56 error ("strjust: input must be a string or character matrix");
57 endif 57 endif
58 58
59 if (isempty (x)) 59 if (isempty (s))
60 y = x; 60 y = s;
61 else 61 else
62 ## Apparently, Matlab considers nulls to be blanks as well; however, does 62 ## Apparently, Matlab considers nulls to be blanks as well; however, does
63 ## not preserve the nulls, but rather converts them to blanks. That's a 63 ## not preserve the nulls, but rather converts them to blanks. That's a
64 ## bit unexpected, but it allows simpler processing, because we can move 64 ## bit unexpected, but it allows simpler processing, because we can move
65 ## just the nonblank characters. So we'll do the same here. 65 ## just the nonblank characters. So we'll do the same here.
66 66
67 [nr, nc] = size (x); 67 [nr, nc] = size (s);
68 ## Find the indices of all nonblanks. 68 ## Find the indices of all nonblanks.
69 nonbl = x != " " & x != "\0"; 69 nonbl = s != " " & s != "\0";
70 [idx, jdx] = find (nonbl); 70 [idx, jdx] = find (nonbl);
71 71
72 if (strcmp (just, "right")) 72 if (strcmp (pos, "right"))
73 ## We wish to find the maximum column index for each row. Because jdx is 73 ## We wish to find the maximum column index for each row. Because jdx is
74 ## sorted, we can take advantage of the fact that assignment is processed 74 ## sorted, we can take advantage of the fact that assignment is processed
75 ## sequentially and for duplicate indices the last value will remain. 75 ## sequentially and for duplicate indices the last value will remain.
76 maxs = nc * ones (nr, 1); 76 maxs = nc * ones (nr, 1);
77 maxs(idx) = jdx; 77 maxs(idx) = jdx;
78 shift = nc - maxs; 78 shift = nc - maxs;
79 elseif (strcmp (just, "left")) 79 elseif (strcmp (pos, "left"))
80 ## See above for explanation. 80 ## See above for explanation.
81 mins = ones (nr, 1); 81 mins = ones (nr, 1);
82 mins(flipud (idx(:))) = flipud (jdx(:)); 82 mins(flipud (idx(:))) = flipud (jdx(:));
83 shift = 1 - mins; 83 shift = 1 - mins;
84 else 84 else
93 ## Adjust the column indices. 93 ## Adjust the column indices.
94 jdx += shift (idx); 94 jdx += shift (idx);
95 95
96 ## Create a blank matrix and position the nonblank characters. 96 ## Create a blank matrix and position the nonblank characters.
97 y = " "(ones (1, nr), ones (1, nc)); 97 y = " "(ones (1, nr), ones (1, nc));
98 y(sub2ind ([nr, nc], idx, jdx)) = x(nonbl); 98 y(sub2ind ([nr, nc], idx, jdx)) = s(nonbl);
99 endif 99 endif
100 100
101 endfunction 101 endfunction
102 102
103 %!error <Invalid call to strjust> strjust(); 103 %!error <Invalid call to strjust> strjust();