home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / strings / str2mat.m < prev    next >
Text File  |  1999-11-19  |  2KB  |  80 lines

  1. ## Copyright (C) 1996 Kurt Hornik
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} str2mat (@var{s_1}, @dots{}, @var{s_n})
  22. ## Return a matrix containing the strings @var{s_1}, @dots{}, @var{s_n} as
  23. ## its rows.  Each string is padded with blanks in order to form a valid
  24. ## matrix.
  25. ## 
  26. ## @strong{Note:}
  27. ## This function is modelled after @sc{Matlab}.  In Octave, you can create
  28. ## a matrix of strings by @code{[@var{s_1}; @dots{}; @var{s_n}]} even if
  29. ## the strings are not all the same length.
  30. ## @end deftypefn
  31.  
  32. ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
  33. ## Adapted-By: jwe
  34.  
  35. function retval = str2mat (...)
  36.  
  37.   if (nargin == 0)
  38.     usage ("str2mat (s1, ...)");
  39.   endif
  40.  
  41.   nc = 0;
  42.   nr = 0;
  43.  
  44.   va_start ();
  45.  
  46.   nr = zeros (nargin, 1);
  47.   nc = zeros (nargin, 1);
  48.   for k = 1 : nargin
  49.     s = va_arg ();
  50.     if (isstr (s))
  51.       [nr(k), nc(k)] = size (s);
  52.     else
  53.       error ("str2mat: all arguments must be strings");
  54.     endif
  55.   endfor
  56.  
  57.   tmp = find (nr == 0);
  58.  
  59.   if (! isempty (tmp))
  60.     nr(tmp) = 1;
  61.   endif
  62.  
  63.   retval_nr = sum (nr);
  64.   retval_nc = max (nc);
  65.  
  66.   retval = setstr (ones (retval_nr, retval_nc) * toascii (" "));
  67.  
  68.   va_start ();
  69.  
  70.   row_offset = 0;
  71.   for k = 1 : nargin
  72.     s = va_arg ();
  73.     if (nc(k) > 0)
  74.       retval ((row_offset + 1) : (row_offset + nr(k)), 1:nc(k)) = s;
  75.     endif
  76.     row_offset = row_offset + nr(k);
  77.   endfor
  78.  
  79. endfunction
  80.