function optsOut = optionsGUI(opts, tooltips)

caller = dbstack;
if length(caller)>1
    caller = caller(2).name;
else
    caller = 'Unknown Function';
end
if nargin<2
    tooltips = [];
end

optsOut = opts;

[optNames, sortorder]= sort(fieldnames(opts)); %#ok<FLPST>
N = length(optNames);
titlesX = 5; titlesW = 80;
etX = titlesX+titlesW+20; etW = 100;
H = 15;
H0 =10;
HOK = 40;

handles.F = figure('Name', caller, 'pos', [600 600 titlesW+etW+40 N*(H+H0)+HOK+5], 'toolbar', 'none', 'menubar', 'none', 'resize', 'off', 'numbertitle', 'off');
handles.OK = uicontrol(...
    'Units','pixels',...
    'Parent',handles.F,...
    'Style','pushbutton',...
    'Position',[etX 5 etW 20],...
    'String','OK',...
    'Callback', @OK);
handles.ET = [];

for n = 1:length(optNames)
    handles.titles(n) = uicontrol(...
        'Units','pixels',...
        'Parent',handles.F,...
        'Style','text',...
        'Position',[titlesX HOK+(n-1)*(H+H0)-4 titlesW H],...
        'String',optNames(n));
    
    handles.reset(n) = uicontrol(...
        'Units','pixels',...
        'Parent',handles.F,...
        'Style','pushbutton',...
        'Position',[etX+etW+5 HOK+(n-1)*(H+H0)-4 10 H],...
        'String','',...
        'Callback', @(varargin)(reset(n)));
    
    reset(n);
end
waitfor(handles.F);

    function parseET(src,n)
        type=class(opts.(optNames{n}));
        set(handles.titles(n), 'ForegroundColor', 'k')
        try
            switch type
                case 'logical'
                    optsOut.(optNames{n}) = eval(src.String{src.Value});
                case 'double'
                    optsOut.(optNames{n}) = eval(['[' src.String ']']);
                case 'cell'
                    optsOut.(optNames{n}) = eval(src.String{src.Value});
                case 'char'
                    optsOut.(optNames{n}) = src.String;
            end
        catch ME
            %make the text error
            set(handles.titles(n), 'ForegroundColor', 'r')
        end
        
    end
    function OK(varargin)
        for k = 1:length(handles.ET)
            parseET(get(handles.ET(k)), k)
        end
        delete(handles.F)
    end
    function reset(n)
        set(handles.titles(n), 'ForegroundColor', 'k')
        optsOut.(optNames{n}) = opts.(optNames{n});
        if length(handles.ET)>=n
            delete(handles.ET(n));
        end
        switch class(opts.(optNames{n}))
            case 'logical' %make a drop down menu
                handles.ET(n) = uicontrol(...
                    'Units','pixels',...
                    'Parent',handles.F,...
                    'Style','popupmenu',...
                    'String', {'false', 'true'},...
                    'Value', double(opts.(optNames{n}))+1,...
                    'Position',[etX HOK+(n-1)*(H+H0) etW H],...
                    'callback', @(src,evnt)(parseET(src, n)));
            case 'double'
                handles.ET(n) = uicontrol(...
                    'Units','pixels',...
                    'Parent',handles.F,...
                    'Style','edit',...
                    'String', num2str(opts.(optNames{n})),...
                    'Position',[etX HOK+(n-1)*(H+H0)-4 etW H],...
                    'callback', @(src,evnt)(parseET(src, n)));
            case 'char'
                handles.ET(n) = uicontrol(...
                    'Units','pixels',...
                    'Parent',handles.F,...
                    'Style','edit',...
                    'String', opts.(optNames{n}),...
                    'Position',[etX HOK+(n-1)*(H+H0)-4 etW H],...
                    'callback', @(src,evnt)(parseET(src, n)));
            case 'cell' %make a drop down
                handles.ET(n) = uicontrol(...
                    'Units','pixels',...
                    'Parent',handles.F,...
                    'Style','popupmenu',...
                    'String', opts.(optNames{n}),...
                    'Value', 1,...
                    'Position',[etX HOK+(n-1)*(H+H0) etW H],...
                    'callback', @(src,evnt)(parseET(src, n)));
            otherwise
                keyboard
        end
        if isfield(tooltips, optNames{n})
            set(handles.titles(n), 'TooltipString', tooltips.(optNames{n}));
            set(handles.ET(n), 'TooltipString', tooltips.(optNames{n}));
        end
    end
end