function tok = str_split(str,sep)

% str_split - split strings using delimiter
% -----------------------------------------
%
% tok = str_split(str,sep)
%
% Input:
% ------
%  str - string to split
%
% Output:
% -------
%  tok - string tokens as defined by delimiter

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 1482 $
% $Date: 2005-08-08 16:39:37 -0400 (Mon, 08 Aug 2005) $
%--------------------------------

%-----------------------------------
% HANDLE INPUT
%-----------------------------------

%--
% set default delimiter
%--

if (nargin < 2)
	sep = ' ';
end

% NOTE: return full string as cell if delimiter is empty

if (isempty(sep))
	tok = {str}; return
end

%-----------------------------------
% SPLIT STRING
%-----------------------------------

%--
% separate string using delimiter
%--

n = length(sep);

switch (n)
	
	%--
	% single character separator
	%--
	
	case (1)
		
		tok = strread(str,'%s','delimiter',sep);

	%--
	% multiple character separator
	%--
	
	otherwise
		
		ix = findstr(str,sep);
		
		if (isempty(ix))
			tok = {str}; return;
		end
		
		ix1 = [1, ix + n];
		
		ix2 = [ix - 1, length(str)];
		
		for k = 1:length(ix1)
			tok{k} = str(ix1(k):ix2(k));
		end
		
end