function [opts, remaining] = options(varargin)
%OPTIONS extract option string parameters from arguments
% [OPTS, REMAINING] = OPTIONS(arg1, ...) extracts the param-value pairs from the
% arguments and returns them in a struct with field names equal to the params

% Copyright (c) Peter Fiala 2012-2014

opts = struct([]);

start = 1;
while start <= nargin
    if ischar(varargin{start})
        break
    end
    start = start + 1;
end

remaining = varargin(1:start-1);

for i = start : 2 : nargin
    arg = varargin{i};
    if ischar(arg)
        opts(1).(arg) = varargin{i+1};
    end
end

end

