Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB help Solution function words = allwords(str,sepchars) if ~isvector(str) e

ID: 3735545 • Letter: M

Question

MATLAB help

Explanation / Answer

function words = allwords(str,sepchars) if ~isvector(str) error('ALLWORDS:nonvectorinput','str must be a vector') end if nargin > 2 error('ALLWORDS:toomanyargs','No more than two arguments allowed') elseif nargin < 1 help allwords return end if (nargin < 2) || isempty(sepchars) if ischar(str) sepchars = [' .,;:?!',char([9 13])]; elseif all((round(str) == str) | isnan(str)) sepchars = [-inf inf nan]; else error('ALLWORDS:improperarg', ... 'str must be character or integer of some class') end end if isempty(str) words = {}; return end sepchars = sepchars(:); str = reshape(str,1,[]); if ~ismember(str(1),sepchars) str = [sepchars(1),str]; end if ~ismember(str(end),sepchars) str = [str,sepchars(1)]; end scind = ismember(str,sepchars); if any(isnan(sepchars)) scind = scind | isnan(str); end wordbegin = strfind(scind,[1 0]); wordend = strfind(scind,[0 1]); wordlengths = wordend - wordbegin; nwords = numel(wordlengths); words = cell(1,nwords); [uniklengths,I,J] = unique(wordlengths); %#ok for ulen = uniklengths k = find(wordlengths == ulen); ind = bsxfun(@plus,wordbegin(k).',1:ulen); if ulen == 1 if ischar(str) words(k) = cellstr(str(ind).'); else words(k) = mat2cell(str(ind).',ones(numel(k),1),ulen); end else if ischar(str) words(k) = cellstr(str(ind)); else words(k) = mat2cell(str(ind),ones(numel(k),1),ulen); end end end