Home > Codi > char2cell.m

char2cell

PURPOSE ^

Syntax: cellmat = char2cell(s,delim,rowseparate,trim);

SYNOPSIS ^

function s = char2cell(s,delim,rowseparate,trim)

DESCRIPTION ^

 Syntax: cellmat = char2cell(s,delim,rowseparate,trim);
   cellmat = char2cell(s,delim,rowseparate);
   cellmat = char2cell(s,delim);
   cellmat = char2cell(s);
 CHARacter array 2(to) CELL array conversion.
   "s" - Input character array or cell array of strings.
   "delim" - Array or cell array of delimiters. 
       If an array then each element is a single element delimeter.
       If a cell array then each cell element is a delimeter or multi-element delimeter.
       The following may be used for non-printing characters:
       (as these characters require 2 elements, they must be specified in a cell array)
           \b - backspace
           \f - formfeed
           \n - linefeed
           \r - carriage return
           \t - tab
           \\ - backslash 
           (a single \ suffices if it is a single or last element)
           Use ' ' to specify a white space character.
       Default delimiter is white space if "rowseparate" is empty and is empty 
       if "rowseparate" is either "true" or "false".
   "rowseparate" - "true" designates that each row should be separated or
       "false" if each column should be separated. Only relevant if "s" is
       multidimensional. Higher dimensions are wrapped into rows or columns
       depending upon "rowseparate". If "empty", then all matrices are treated as 1D.
       e.g. a KxMxN matrix is treated as a Kx(MxN) matrix if "false"
           or as a Mx(KxN) if "true".
       If a delimeter(s) is specified then both conditions are used.
       "rowseparate" is ignored if "s" is a cell array.
   "trim" - if "true" (default), leading and trailing spaces are deleted 
   "cellmat" - Output (1D) cell array.   

   See Also: cellstr, mat2cell, num2cell, cell2mat
   Examples:
       char2cell(['red? green33 blue++  '],['?3+']))
       ans =     'red'; 'green'; 'blue'; ''
       c=sprintf(['this is a test\nthis\tis the second line'])
       char2cell(c,{'\n','\t'})
       ans =       'this is a test'
                   'this'
                   'is the second line'

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function s = char2cell(s,delim,rowseparate,trim)
0002 % Syntax: cellmat = char2cell(s,delim,rowseparate,trim);
0003 %   cellmat = char2cell(s,delim,rowseparate);
0004 %   cellmat = char2cell(s,delim);
0005 %   cellmat = char2cell(s);
0006 % CHARacter array 2(to) CELL array conversion.
0007 %   "s" - Input character array or cell array of strings.
0008 %   "delim" - Array or cell array of delimiters.
0009 %       If an array then each element is a single element delimeter.
0010 %       If a cell array then each cell element is a delimeter or multi-element delimeter.
0011 %       The following may be used for non-printing characters:
0012 %       (as these characters require 2 elements, they must be specified in a cell array)
0013 %           \b - backspace
0014 %           \f - formfeed
0015 %           \n - linefeed
0016 %           \r - carriage return
0017 %           \t - tab
0018 %           \\ - backslash
0019 %           (a single \ suffices if it is a single or last element)
0020 %           Use ' ' to specify a white space character.
0021 %       Default delimiter is white space if "rowseparate" is empty and is empty
0022 %       if "rowseparate" is either "true" or "false".
0023 %   "rowseparate" - "true" designates that each row should be separated or
0024 %       "false" if each column should be separated. Only relevant if "s" is
0025 %       multidimensional. Higher dimensions are wrapped into rows or columns
0026 %       depending upon "rowseparate". If "empty", then all matrices are treated as 1D.
0027 %       e.g. a KxMxN matrix is treated as a Kx(MxN) matrix if "false"
0028 %           or as a Mx(KxN) if "true".
0029 %       If a delimeter(s) is specified then both conditions are used.
0030 %       "rowseparate" is ignored if "s" is a cell array.
0031 %   "trim" - if "true" (default), leading and trailing spaces are deleted
0032 %   "cellmat" - Output (1D) cell array.
0033 %
0034 %   See Also: cellstr, mat2cell, num2cell, cell2mat
0035 %   Examples:
0036 %       char2cell(['red? green33 blue++  '],['?3+']))
0037 %       ans =     'red'; 'green'; 'blue'; ''
0038 %       c=sprintf(['this is a test\nthis\tis the second line'])
0039 %       char2cell(c,{'\n','\t'})
0040 %       ans =       'this is a test'
0041 %                   'this'
0042 %                   'is the second line'
0043 
0044 %   Copyright 2009 Mirtech, Inc.
0045 %   Created by Mirko Hrovat     07/17/2009
0046 %   Inspired by str2cell.m (File Exchange #4247) by us
0047 %------------------------------------------------------------------------------------
0048 
0049 ws = ' ';   % whitespace
0050 bspc = 8;   % \b
0051 ff = 12;    % \f
0052 lf = 10;    % \n
0053 cr = 13;    % \r
0054 tb = 9;     % \t
0055 bs = 92;    % \\
0056 del = 127;  %#ok - currently not used, delete
0057 spc = 32;   % space
0058 bell = 8;   %#ok - currently not used, bell
0059 def_delim   = ws;       % default delimiter
0060 def_trim    = true;     % default trim value
0061 switch nargin
0062     case 4
0063     case 3
0064         trim = [];
0065     case 2
0066         trim = [];      rowseparate = [];
0067     case 1
0068         trim = [];      rowseparate = [];       delim = [];
0069     otherwise
0070         error ('  Number of input arguments incorrect!')
0071 end
0072 if isempty(trim),       trim = def_trim;        end
0073 if ~ischar(s)&&~iscellstr(s),
0074     error ('  Input array must be a character or cell string  array!')
0075 end
0076 if ~iscellstr(s),
0077     if isempty(rowseparate) || size(s,1)==1 || size(s,2)==1,
0078         s = shiftdim(s(:),-1);          % convert to row of characters
0079         rowseparate = [];
0080     else
0081         if ~rowseparate,
0082             s = permute(s,[2,1]);
0083         end
0084         s = s(:,:);                     % make s a 2D array
0085         s = cellstr(s);                 % now convert rows to cells
0086     end
0087     if isempty(rowseparate) && isempty(delim),
0088         delim = def_delim;
0089     end
0090 else
0091     if isempty(delim),      delim = def_delim;      end
0092 end
0093 if ~isempty(delim),
0094     if ~iscell(delim),  delim = num2cell(delim);    end
0095     strtidx = [];
0096     stopidx = [];
0097     for n = 1:numel(delim),
0098         mpts = numel(delim{n});
0099         searchchar = char(ones(1,mpts)*spc);
0100         m = 0;
0101         nschars = 0;
0102         while m < mpts,
0103             m = m + 1;
0104             curchar = delim{n}(m);
0105             if curchar=='\'
0106                 m = m + 1;
0107                 if m <= mpts,
0108                     curchar = delim{n}(m);
0109                     switch curchar
0110                         case 'b'    % backspace
0111                             curchar = char(bspc);
0112                         case 'f'    % formfeed
0113                             curchar = char(ff);
0114                         case 'n'    % linefeed
0115                             curchar = char(lf);
0116                         case 'r'    % return
0117                             curchar = char(cr);
0118                         case 't'    % tab
0119                             curchar = char(tb);
0120                         case '\'    % backslash
0121                             curchar = char(bs);
0122                         otherwise
0123                             error('  Special character not recognized, e.g. \n !')
0124                     end
0125                 else    % backslash is a single element or is last element
0126                     curchar = char(bs); % so intepret it as a backslash
0127                 end
0128             end
0129             nschars = nschars + 1;
0130             searchchar(nschars) = curchar;
0131         end
0132         searchchar(nschars+1:end) = [];
0133         tmp = strfind(s,searchchar);        % find matching indices
0134         if iscell(tmp),
0135             stopidx = strcat(stopidx,tmp);      % combine results
0136             tmp2 = num2cell(ones(size(s))*nschars);
0137             % add delimiter length to get next starting indices
0138             tmp2 = cellfun(@plus,tmp,tmp2,'UniformOutput',false); 
0139             strtidx = strcat(strtidx,tmp2);     % combine results
0140         else
0141             stopidx = [stopidx,tmp];            % combine results
0142             tmp2 = tmp + nschars;               % add delimiter length
0143             strtidx = [strtidx,tmp2];            
0144         end
0145     end
0146         
0147     % now use strt and stop idx to create cells
0148     if iscell(s),
0149         ncells = sum(cellfun(@numel,strtidx));  % find total number of indices
0150         scells = size(s,1);
0151         tmp = cell(ncells,1);
0152         count = 0;
0153         for m = 1:scells,
0154             startpt = 1;
0155             strt = sort(strtidx{m});
0156             stop = sort(stopidx{m});
0157             for p=1:numel(strt),
0158                 if stop(p)>startpt,
0159                     count = count + 1;
0160                     tmp{count} = s{m}(startpt:stop(p)-1);
0161                 end
0162                 startpt = strt(p);
0163             end
0164             if startpt <= numel(s{m}),      % need to extract the rest of the array
0165                 count = count + 1;
0166                 tmp{count} = s{m}(startpt:end);
0167             end
0168         end
0169     else            % s is not a cell array
0170         strt = sort(strtidx);
0171         stop = sort(stopidx );
0172         ncells = numel(strtidx);
0173         tmp = cell(ncells+1,1);
0174         startpt = 1;
0175         count = 0;
0176         for m = 1:ncells
0177             if stop(m) > startpt,
0178                 count = count + 1;
0179                 tmp{count}= s(startpt:stop(m)-1);   
0180             end
0181             startpt = strt(m); 
0182         end
0183         if startpt <= numel(s),     % need to extract the rest of the array
0184             count = count + 1;
0185             tmp{count} = s(startpt:end);
0186         end
0187     end
0188     s = tmp(1:count);
0189 end
0190 if trim,
0191     s = strtrim(s);
0192 end

Generated on Wed 12-Sep-2012 13:03:54 by m2html © 2005