annotate aim-mat/tools/str2cell.m @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents 20ada0af3d7d
children
rev   line source
tomwalters@0 1 % tool
tomwalters@0 2 %
tomwalters@0 3 % INPUT VALUES:
tomwalters@0 4 %
tomwalters@0 5 % RETURN VALUE:
tomwalters@0 6 %
tomwalters@0 7 %
bleeck@3 8 % This external file is included as part of the 'aim-mat' distribution package
bleeck@3 9 % (c) 2011, University of Southampton
bleeck@3 10 % Maintained by Stefan Bleeck (bleeck@gmail.com)
bleeck@3 11 % download of current version is on the soundsoftware site:
bleeck@3 12 % http://code.soundsoftware.ac.uk/projects/aimmat
bleeck@3 13 % documentation and everything is on http://www.acousticscale.org
bleeck@3 14
tomwalters@0 15
tomwalters@0 16 function c = str2cell(varargin)
tomwalters@0 17 %STR2CELL Convert a string to a cell array of lines.
tomwalters@0 18 %
tomwalters@0 19 % C = STR2CELL(STR) creates a cell array C where each cell contains a line
tomwalters@0 20 % of the string STR.
tomwalters@0 21 %
tomwalters@0 22 % C = STR2CELL(STR, OPTS), where OPTS is 'L', 'T' or both, removes leading
tomwalters@0 23 % and/or trailing blank lines from the string before converting to a cell
tomwalters@0 24 % array.
tomwalters@0 25 %
tomwalters@0 26 % If the string contains LFs (linefeed characters, decimal 10), the input
tomwalters@0 27 % string is split at their position after all CRs (carriage return
tomwalters@0 28 % characters, decimal 13) have been removed. If there are no LFs, the
tomwalters@0 29 % string is split at the position of the CRs. This should ensure that the
tomwalters@0 30 % string is split correctly with both UNIX (LF), DOS (CR+LF) and MAC (CR)
tomwalters@0 31 % definitions of a newline.
tomwalters@0 32
tomwalters@0 33 % Author: Peter J. Acklam
tomwalters@0 34 % Time-stamp: 2002-03-03 13:44:44 +0100
tomwalters@0 35 % E-mail: pjacklam@online.no
tomwalters@0 36 % URL: http://home.online.no/~pjacklam
tomwalters@0 37
tomwalters@0 38 error(nargchk(1, 3, nargin));
tomwalters@0 39
tomwalters@0 40 %
tomwalters@0 41 % Assign default values to parameters that can be changed by command line
tomwalters@0 42 % options.
tomwalters@0 43 %
tomwalters@0 44 strip_lead = 0;
tomwalters@0 45 strip_trail = 0;
tomwalters@0 46
tomwalters@0 47 %
tomwalters@0 48 % Process command line options.
tomwalters@0 49 %
tomwalters@0 50 % while length(varargin) > 1
tomwalters@0 51 % opt = varargin{2};
tomwalters@0 52 % if ~ischar(opt)
tomwalters@0 53 % error('Options must be strings.');
tomwalters@0 54 % end
tomwalters@0 55 % switch opt
tomwalters@0 56 % case { 'l', 'L' }
tomwalters@0 57 % strip_lead = 1;
tomwalters@0 58 % case { 'u', 'U' }
tomwalters@0 59 % strip_trail = 1;
tomwalters@0 60 % otherwise
tomwalters@0 61 % error([ 'Unknown option: ' opt ]);
tomwalters@0 62 % end
tomwalters@0 63 % varargin(2) = [];
tomwalters@0 64 % end
tomwalters@0 65 str = varargin{1};
tomwalters@0 66
tomwalters@0 67 %
tomwalters@0 68 % Strip leading blank lines.
tomwalters@0 69 %
tomwalters@0 70 if strip_lead
tomwalters@0 71 k = find(~isspace(str));
tomwalters@0 72 if ~isempty(k)
tomwalters@0 73 k = min(k);
tomwalters@0 74 str = str(k:end);
tomwalters@0 75 end
tomwalters@0 76 end
tomwalters@0 77
tomwalters@0 78 %
tomwalters@0 79 % Strip trailing blank lines.
tomwalters@0 80 %
tomwalters@0 81 if strip_trail
tomwalters@0 82 k = find(~isspace(str));
tomwalters@0 83 if ~isempty(k)
tomwalters@0 84 k = max(k);
tomwalters@0 85 str = str(1:k);
tomwalters@0 86 end
tomwalters@0 87 end
tomwalters@0 88
tomwalters@0 89 %
tomwalters@0 90 % Quick exit if string is empty.
tomwalters@0 91 %
tomwalters@0 92 if isempty(str)
tomwalters@0 93 c = { '' };
tomwalters@0 94 return
tomwalters@0 95 end
tomwalters@0 96
tomwalters@0 97 %
tomwalters@0 98 % Find the characters that separate the lines.
tomwalters@0 99 %
tomwalters@0 100
tomwalters@0 101 % just a hack from me... sorry... no time
tomwalters@0 102
tomwalters@0 103 % strip []
tomwalters@0 104 if str(1)=='['
tomwalters@0 105 str=str(2:end-1);
tomwalters@0 106 end
tomwalters@0 107 k = find(str == 10); % find all LF chars
tomwalters@0 108 l = find(str == 13); % find all CR chars
tomwalters@0 109 m=find(str==' ');
tomwalters@0 110 if isempty(m)
tomwalters@0 111 m=length(str)+1;
tomwalters@0 112 end
tomwalters@0 113 if isempty(k) % if no LF chars were found
tomwalters@0 114 k = l; % split at CR chars
tomwalters@0 115 else % or else
tomwalters@0 116 if ~isempty(l) % if there are CR chars
tomwalters@0 117 str(l) = []; % remove them
tomwalters@0 118 k = find(str == 10); % find all LF chars
tomwalters@0 119 end
tomwalters@0 120 end
tomwalters@0 121
tomwalters@0 122 %
tomwalters@0 123 % Avoid empty last string in output list when string ends in a newline.
tomwalters@0 124 %
tomwalters@0 125 if ~isempty(k) & k(end) == length(str)
tomwalters@0 126 k = [ 0 k ]; % add beginning
tomwalters@0 127 else
tomwalters@0 128 k = [ 0 k length(str)+1 ]; % add beginning and end
tomwalters@0 129 end
tomwalters@0 130
tomwalters@0 131 %
tomwalters@0 132 % Now split the string into lines.
tomwalters@0 133 %
tomwalters@0 134 n = length(m); % number of lines
tomwalters@0 135 c = cell(n,1); % initialize output
tomwalters@0 136 m=[0 m];
tomwalters@0 137 for i = 1:n
tomwalters@0 138 c{i} = str(m(i)+1 : m(i+1)-1); % extract line
tomwalters@0 139 end
tomwalters@0 140
tomwalters@0 141
tomwalters@0 142