% -----------------------------------------------------------------------
% --- read a GUI with handle 'handle' producing a structure 'struct'
function struct=read_GUI(handle)
%------------------------------------------------------------------------
struct=[];%default
hchild=get(handle,'children');
for ichild=1:numel(hchild)
    if strcmp(get(hchild(ichild),'Visible'),'on')
        object_type=get(hchild(ichild),'Type');
        tag=get(hchild(ichild),'tag');
        switch object_type
            case 'uipanel'
                eval(['struct.' tag '=read_GUI(hchild(ichild));'])
            case 'uicontrol'
                object_style=get(hchild(ichild),'Style');
                check_input=1;%default
                switch object_style
                    case {'checkbox','radiobutton','togglebutton'}
                        input=get(hchild(ichild),'Value');
                    case 'edit'
                        separator=regexp(tag,'^num_');
                        if isempty(separator)
                            input=get(hchild(ichild),'String');
                        else
                            input=str2double(get(hchild(ichild),'String'));
                            tag=regexprep(tag,'^num_','');
                            %deal with undefined input: retrieve the default value stored as UserData
                            if isnan(input)
                                input=get(hchild(ichild),'UserData');
                                set(hchild(ichild),'String',num2str(input))
                            end
                        end                        
                    case{'listbox','popupmenu'}
                        listinput=get(hchild(ichild),'String');
                        value=get(hchild(ichild),'Value');
                        if ~isempty(listinput)
                            input=listinput{value};
                        else
                            check_input=0;
                        end
                        separator=regexp(tag,'^num_');
                        if ~isempty(separator)
                            input=str2double(input);% transform to numerical values if the uicontrol tag begins with 'num_'
                            tag=regexprep(tag,'^num_','');
                        end
                    otherwise
                        check_input=0;
                end
                if check_input
                    struct.(tag)=input;
                end
            case 'uitable'
                struct.(tag)=get(hchild(ichild),'Data');
        end
    end
end
