Parse.m

From Pnb
Jump to navigation Jump to search
%PARSE2.M
%Takes a string and returns val token.
%Returns [] if val token does not exist.
%Returns val token and rest of line if val<0 including any blanks.
%returns cell array of all tokens if val==0.

%does the same thing as parse.m in a slightly different way.

%useage: out=parse(string,val)

function [out,rem]=parse(string,val);

out=[];
cout={};
maxline=0;
for irow=1:size(string,1),
  p=[];
  r=string(irow,:);
  while ~isempty(r),
    [t,r]=strtok(r);
    p=strvcat(p,t);
  end
  prow{irow}=p;
  %pad lines with blanks to make all matrices on row same length
  maxline=max(maxline,size(p,1));
end
for irow=1:size(string,1),
  p=prow{irow};
  %pad lines with blanks to make all matrices on row same length
  nline=maxline-size(p,1);
  for iline=1:nline,
    p=strvcat(p,' ');
  end
  cout=[cout cellstr(p)];
end
cout=cout';
if abs(val)<=size(cout,2),
  if val<0,
    if length(val)~=1,
      error('parse.m: for val < 1, length(val) == 1.');
    end
    val=abs(val):size(cout,2);
  end
  if val~=0,
    for irow=1:size(cout,1),
      tout=[];
      for icol=val,
	tout=[tout cout{irow,icol} ' ' ];
      end
      out=strvcat(out,tout);
    end
    out=deblank(out);
  else,
    out=cout;
  end
end

return
%TODO
%make work for string arrays with different numbers of words on
%each line.

%if no val field put in an empty cell

%add rem output of (val+1):end as a string

parse