Code covered by the BSD License  

Highlights from
JPEG2000

from JPEG2000 by Peter Rydesäter
Reads and writes files in the new JPEG 2000 image format.

[st,st2,optexist]=parse_parameter_list(varargin)
% PARSE_PARAMETER_LIST  Read/convert/return argument lists in various formats.
%
% Make a parameter structure of a parameter list:
%   st=parse_parameter_list({'parameter1','value1','parameter2','value2'....})
%
% Make a parameter structure of a parameter list:
%   st=parse_parameter_list({'parameter1=value1','parameter2=value2'....})
%
% Separate parameters of parameter list to second structure:
%   [st,st2]=parse_parameter_list({'parameter1','value1','parameter2','value2'....},'seppar1',.... )
%
% Separate parameters of parameter structure to second structure:
%   [st,st2]=parse_parameter_list(st,'seppar1',.... )
%
% Make a parameter list of a parameter structure:
%   lst=parse_parameter_list('cell',st)
%
% Convert strings to numbers in struct if possible:
%   lst=parse_parameter_list('str2num',st)
%
%
%
% 2002-11-06 Peter Rydester
%

function [st,st2,optexist]=parse_parameter_list(varargin)
  st2=struct([]);
  exist=[];
  if ischar(varargin{1})
    mode=varargin{1};
    indata=varargin{2};
    varargin=varargin(3:end);
  else
    mode='';
    indata=varargin{1};
    varargin=varargin(2:end);
  end
  if ischar(indata)
    lst={};
    indata=local_trim(indata);
    while length(indata),
      [arg,indata]=strtok(indata);
      lst{end+1}=local_trim(arg);      
      indata=local_trim(indata);      
    end
    indata=lst;
  end
  if isstruct(indata),
    st=indata;
  elseif iscell(indata),
    lst={};
    for arg=indata,
      arg=arg{1};
      if isnumeric(arg), arg=num2str(arg); end
      idx=find(arg=='=');
      if isempty(idx),
	lst{end+1}=arg;
      else
	lst{end+1}=arg(1:idx(1)-1);
	lst{end+1}=arg(idx(1)+1:end);
      end
    end
    st=struct(lst{:});
  end 
  for arg=varargin,
    arg=arg{1};
    if isfield(st,arg),
      eval( ['st2(1).' arg '=getfield(st,arg);'] );
      st=rmfield(st,arg);
      eval( ['optexist(1).' arg '=1;'] );
    else
      eval( ['optexist(1).' arg '=0;'] );
    end
  end

  switch mode
   case {'struct' ''}
    %Just pass out as is
   case 'cell'
    lst=cell(1,length(fieldnames(st))*2);
    lst(1:2:end)=fieldnames(st);
    lst(2:2:end)=struct2cell(st);
    st=lst;
   case 'str2num'
    st=strfields2num(st);
   case 'num2str'
    st=strfields2num(st);
   case 'jasper'
    if length(fieldnames(st))==0;
      st='';
    else
      st=numfields2str(st);
      st=parse_parameter_list('cell',st);
      st=sprintf(' -O %s=%s ',st{:});
    end
  end
  return
  
function st=strfields2num(st)
  for fld=fieldnames(st)',
    value=getfield(st,fld{1});
    if ischar(value) & length(str2num(value)),
      st=setfield(st,fld{1},str2num(value));
    end
  end
  return;
  
function st=numfields2str(st)
  for fld=fieldnames(st)',
    value=getfield(st,fld{1});
    if isnumeric(value),
      st=setfield(st,fld{1},num2str(value,'%.20g'));
    end
  end
  return;

function str=local_trim(str)
  str=deblank(str(end:-1:1));
  str=deblank(str(end:-1:1));
  return;

Contact us at files@mathworks.com