function varargout = OwnProblem(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @OwnProblem_OpeningFcn, ...
    'gui_OutputFcn',  @OwnProblem_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

function OwnProblem_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
movegui(hObject,'center');
clearvars -global genfiles gennames genpaths;

function varargout = OwnProblem_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;

function listbox1_Callback(hObject, eventdata, handles)
global genfiles;
items=get(handles.listbox1,'String');
if ~isempty(items)
    selitem=items{get(handles.listbox1,'Value')};
    for i=1:length(genfiles)
        name{i}=fieldnames(genfiles{i});
        names{i}=name{i}{1};
    end
    ind=find(ismember(names,selitem));
    eval(['data=genfiles{ind}.' selitem ';']);
    [datadim,datasizes]=dimsizetype(data);
    set(handles.edit1,'String',[num2str(datadim) 'D']);
    set(handles.edit2,'String',datasizes);
end

function listbox1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function pushbutton1_Callback(hObject, eventdata, handles)
global genfiles gennames genpaths;
[filename,filepath,filterindex] = uigetfile('*.mat','Multiselect','on');
if (~isequal(filename, 0) && ~iscell(filename))
    filedest=[filepath filename];
    if isempty(genpaths)
        files={load(filedest)};
        namestr={filename(1:end-4)};
        genfiles=[genfiles files];
        gennames=[gennames namestr];
        genpaths={filedest};
    else
        for i=1:length(genpaths)
            if isequal(genpaths{i},filedest)
                match(i)=1;
            else
                match(i)=0;
            end
        end
        if sum(match)==0
            files={load(filedest)};
            namestr={filename(1:end-4)};
            genfiles=[genfiles files];
            gennames=[gennames namestr];
            genpaths=[genpaths filedest];
        end
    end
elseif (~isequal(filename, 0) && iscell(filename))
    for i=1:length(filename)
        filedest{i}=[filepath filename{i}];
    end
    if isempty(genpaths)
        for i=1:length(filename)
            files{i}=load(filedest{i});
            namestr{i}=filename{i}(1:end-4);
            genfiles=[genfiles {files{i}}];
            gennames=[gennames {namestr{i}}];
        end
        genpaths=[genpaths filedest];
    else
        for i=1:length(genpaths)
            for j=1:length(filedest)
                if isequal(genpaths{i},filedest{j})
                    match(i,j)=1;
                else
                    match(i,j)=0;
                end
            end
        end
        if sum(match)==0
            for i=1:length(filename)
                files{i}=load(filedest{i});
                namestr{i}=filename{i}(1:end-4);
                genfiles=[genfiles {files{i}}];
                gennames=[gennames {namestr{i}}];
            end
            genpaths=[genpaths filedest];
        end
    end
end
set(handles.listbox1,'String',gennames);
items=get(handles.listbox1,'String');
if ~isempty(items)
    name=fieldnames(genfiles{1});
    names=name{1};
    eval(['data=genfiles{1}.' names ';']);
    [datadim,datasizes]=dimsizetype(data);
    set(handles.edit1,'String',[num2str(datadim) 'D']);
    set(handles.edit2,'String',datasizes);
end

function pushbutton2_Callback(hObject, eventdata, handles)
global genfiles modes status;
items=get(handles.listbox1,'String');
[chk,modes]=loadownerr(items,genfiles);
if chk==0
    clearvars -global genfiles gennames genpaths;
    close(handles.figure4);
    status=1;
end

function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function pushbutton3_Callback(hObject, eventdata, handles)
global status;
clearvars -global genfiles gennames genpaths modes;
close(handles.figure4);
status=0;
