tomwalters@0: % tool tomwalters@0: % tomwalters@0: % INPUT VALUES: tomwalters@0: % tomwalters@0: % RETURN VALUE: tomwalters@0: % tomwalters@0: % bleeck@3: % This external file is included as part of the 'aim-mat' distribution package bleeck@3: % (c) 2011, University of Southampton bleeck@3: % Maintained by Stefan Bleeck (bleeck@gmail.com) bleeck@3: % download of current version is on the soundsoftware site: bleeck@3: % http://code.soundsoftware.ac.uk/projects/aimmat bleeck@3: % documentation and everything is on http://www.acousticscale.org bleeck@3: tomwalters@0: tomwalters@0: function c = str2cell(varargin) tomwalters@0: %STR2CELL Convert a string to a cell array of lines. tomwalters@0: % tomwalters@0: % C = STR2CELL(STR) creates a cell array C where each cell contains a line tomwalters@0: % of the string STR. tomwalters@0: % tomwalters@0: % C = STR2CELL(STR, OPTS), where OPTS is 'L', 'T' or both, removes leading tomwalters@0: % and/or trailing blank lines from the string before converting to a cell tomwalters@0: % array. tomwalters@0: % tomwalters@0: % If the string contains LFs (linefeed characters, decimal 10), the input tomwalters@0: % string is split at their position after all CRs (carriage return tomwalters@0: % characters, decimal 13) have been removed. If there are no LFs, the tomwalters@0: % string is split at the position of the CRs. This should ensure that the tomwalters@0: % string is split correctly with both UNIX (LF), DOS (CR+LF) and MAC (CR) tomwalters@0: % definitions of a newline. tomwalters@0: tomwalters@0: % Author: Peter J. Acklam tomwalters@0: % Time-stamp: 2002-03-03 13:44:44 +0100 tomwalters@0: % E-mail: pjacklam@online.no tomwalters@0: % URL: http://home.online.no/~pjacklam tomwalters@0: tomwalters@0: error(nargchk(1, 3, nargin)); tomwalters@0: tomwalters@0: % tomwalters@0: % Assign default values to parameters that can be changed by command line tomwalters@0: % options. tomwalters@0: % tomwalters@0: strip_lead = 0; tomwalters@0: strip_trail = 0; tomwalters@0: tomwalters@0: % tomwalters@0: % Process command line options. tomwalters@0: % tomwalters@0: % while length(varargin) > 1 tomwalters@0: % opt = varargin{2}; tomwalters@0: % if ~ischar(opt) tomwalters@0: % error('Options must be strings.'); tomwalters@0: % end tomwalters@0: % switch opt tomwalters@0: % case { 'l', 'L' } tomwalters@0: % strip_lead = 1; tomwalters@0: % case { 'u', 'U' } tomwalters@0: % strip_trail = 1; tomwalters@0: % otherwise tomwalters@0: % error([ 'Unknown option: ' opt ]); tomwalters@0: % end tomwalters@0: % varargin(2) = []; tomwalters@0: % end tomwalters@0: str = varargin{1}; tomwalters@0: tomwalters@0: % tomwalters@0: % Strip leading blank lines. tomwalters@0: % tomwalters@0: if strip_lead tomwalters@0: k = find(~isspace(str)); tomwalters@0: if ~isempty(k) tomwalters@0: k = min(k); tomwalters@0: str = str(k:end); tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: % tomwalters@0: % Strip trailing blank lines. tomwalters@0: % tomwalters@0: if strip_trail tomwalters@0: k = find(~isspace(str)); tomwalters@0: if ~isempty(k) tomwalters@0: k = max(k); tomwalters@0: str = str(1:k); tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: % tomwalters@0: % Quick exit if string is empty. tomwalters@0: % tomwalters@0: if isempty(str) tomwalters@0: c = { '' }; tomwalters@0: return tomwalters@0: end tomwalters@0: tomwalters@0: % tomwalters@0: % Find the characters that separate the lines. tomwalters@0: % tomwalters@0: tomwalters@0: % just a hack from me... sorry... no time tomwalters@0: tomwalters@0: % strip [] tomwalters@0: if str(1)=='[' tomwalters@0: str=str(2:end-1); tomwalters@0: end tomwalters@0: k = find(str == 10); % find all LF chars tomwalters@0: l = find(str == 13); % find all CR chars tomwalters@0: m=find(str==' '); tomwalters@0: if isempty(m) tomwalters@0: m=length(str)+1; tomwalters@0: end tomwalters@0: if isempty(k) % if no LF chars were found tomwalters@0: k = l; % split at CR chars tomwalters@0: else % or else tomwalters@0: if ~isempty(l) % if there are CR chars tomwalters@0: str(l) = []; % remove them tomwalters@0: k = find(str == 10); % find all LF chars tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: % tomwalters@0: % Avoid empty last string in output list when string ends in a newline. tomwalters@0: % tomwalters@0: if ~isempty(k) & k(end) == length(str) tomwalters@0: k = [ 0 k ]; % add beginning tomwalters@0: else tomwalters@0: k = [ 0 k length(str)+1 ]; % add beginning and end tomwalters@0: end tomwalters@0: tomwalters@0: % tomwalters@0: % Now split the string into lines. tomwalters@0: % tomwalters@0: n = length(m); % number of lines tomwalters@0: c = cell(n,1); % initialize output tomwalters@0: m=[0 m]; tomwalters@0: for i = 1:n tomwalters@0: c{i} = str(m(i)+1 : m(i+1)-1); % extract line tomwalters@0: end tomwalters@0: tomwalters@0: tomwalters@0: