function varargout = AddDatasetUCITxyz(varargin)
% ADDDATASETUCITXYZ M-file for AddDatasetUCITxyz.fig
%      ADDDATASETUCITXYZ, by itself, creates a new ADDDATASETUCITXYZ or raises the existing
%      singleton*.
%
%      H = ADDDATASETUCITXYZ returns the handle to a new ADDDATASETUCITXYZ or the handle to
%      the existing singleton*.
%
%      ADDDATASETUCITXYZ('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ADDDATASETUCITXYZ.M with the given input arguments.
%
%      ADDDATASETUCITXYZ('Property','Value',...) creates a new ADDDATASETUCITXYZ or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before AddDatasetUCITxyz_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to AddDatasetUCITxyz_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
 
% Edit the above text to modify the response to help AddDatasetUCITxyz
 
% Last Modified by GUIDE v2.5 16-May-2007 13:28:19
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @AddDatasetUCITxyz_OpeningFcn, ...
                   'gui_OutputFcn',  @AddDatasetUCITxyz_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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
% End initialization code - DO NOT EDIT
 
 
% --- Executes just before AddDatasetUCITxyz is made visible.
function AddDatasetUCITxyz_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to AddDatasetUCITxyz (see VARARGIN)
 
% Choose default command line output for AddDatasetUCITxyz
 
handles.output=hObject;
 
handles.DataProperties=varargin{2};
handles.PathName=varargin{4};
handles.FileName=varargin{6};
handles.NrAvailableDatasets=varargin{8};
 
PutInCentre(hObject);
 
handles.FileInfo=load([handles.PathName handles.FileName]);

mmax=size(handles.FileInfo.d.X,1);
nmax=size(handles.FileInfo.d.X,2);
%nrcolumns=FileInfo.Field(1).Size(2);
%nrblocks=size(FileInfo.Field,2);

% for i=1:nrcolumns
%     str{i}=['Column ' num2str(i)];
% end
% set(handles.SelectX,'String',str);
% clear str;
% 
% for i=1:nrcolumns
%     str{i}=['Column ' num2str(i)];
% end
% set(handles.SelectY,'String',str);
% clear str;

% for i=1:nrcolumns-2
%     str{i}=FileInfo.Field(1).ColLabels{i+2};
%     if size(str{i},2)==0
%         str{i}=['Column ' num2str(i+2)];
%     end
% end
% set(handles.SelectParameter,'String',str);
% clear str

% for i=1:nrblocks
%     str{i}=FileInfo.Field(i).Name;
% end
% set(handles.SelectBlock,'String',str);
% clear str

handles.M=0;
handles.N=0;
% handles.Block=1;
% handles.DateTime=0;
handles.AllM=1;
handles.AllN=1;

set(handles.TextMMax,'String',num2str(mmax));
set(handles.TextNMax,'String',num2str(nmax));
set(handles.ToggleAllM,'Value',handles.AllM);
set(handles.ToggleAllN,'Value',handles.AllN);

handles=RefreshOptions(handles);
handles=RefreshDatasetName(handles);

set(handles.SelectParameter,'Enable','off','BackGroundColor',[0.831 0.816 0.784]);
set(handles.TextParameter,'Enable','off');
set(handles.SelectBlock,'Enable','off','BackGroundColor',[0.831 0.816 0.784]);
set(handles.TextBlock,'Enable','off');

set(handles.EditM,'String','1');
set(handles.EditN,'String','1');

set(handles.EditDatasetName,'String',handles.FileName);

clear handles.FileInfo;

% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes AddDatasetUCITxyz wait for user response (see UIRESUME)
uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = AddDatasetUCITxyz_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Get default command line output from handles structure
varargout{1} = handles.output;
varargout{2} = handles.second_output;
 
close(handles.figure1)
 
% --- Executes during object creation, after setting all properties.
function SelectParameter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SelectParameter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
 
 
 
% --- Executes on selection change in SelectParameter.
function SelectParameter_Callback(hObject, eventdata, handles)
% hObject    handle to SelectParameter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: contents = get(hObject,'String') returns SelectParameter contents as cell array
%        contents{get(hObject,'Value')} returns selected item from SelectParameter
 
handles=RefreshDatasetName(handles);
 
guidata(hObject, handles);
 
 
% --- Executes on button press in OKDataset.
function OKDataset_Callback(hObject, eventdata, handles)
% hObject    handle to OKDataset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
 
handles.output=handles.DataProperties;
handles.second_output=handles.NrAvailableDatasets;
clear handles.FileInfo;
guidata(hObject, handles);
 
uiresume;
 
 
% --- Executes on button press in CancelDataset.
function CancelDataset_Callback(hObject, eventdata, handles)
% hObject    handle to CancelDataset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
handles.output=handles.DataProperties;
handles.second_output=handles.NrAvailableDatasets;
 
guidata(hObject, handles);
 
uiresume;
 
% --- Executes during object creation, after setting all properties.
function SelectBlock_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SelectBlock (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
 
 
% --- Executes on selection change in SelectBlock.
function SelectBlock_Callback(hObject, eventdata, handles)
% hObject    handle to SelectBlock (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: contents = get(hObject,'String') returns SelectBlock contents as cell array
%        contents{get(hObject,'Value')} returns selected item from SelectBlock
 
handles.Block=get(hObject,'Value');
handles=RefreshDatasetName(handles);
guidata(hObject, handles);
 
 
% --- Executes on button press in AddDataset.
function AddDataset_Callback(hObject, eventdata, handles)
% hObject    handle to AddDataset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
name=get(handles.EditDatasetName,'String');
 
if NewDatasetName(handles.DataProperties,handles.NrAvailableDatasets,name)
 
    handles.NrAvailableDatasets=handles.NrAvailableDatasets+1;
 
    nr=handles.NrAvailableDatasets;
 
    handles.DataProperties(nr).Name=get(handles.EditDatasetName,'String');
 
    handles.DataProperties(nr).FileType='UCITxyz';
 
%     i=get(handles.SelectParameter,'Value');
%     str=get(handles.SelectParameter,'String');
%     handles.DataProperties(nr).Parameter=str{i};
     handles.DataProperties(nr).Parameter='unknown';
 
     handles.DataProperties(nr).DateTime=0;
 
%     handles.DataProperties(nr).Block=get(handles.SelectBlock,'Value');

%     handles.DataProperties(nr).Column=get(handles.SelectParameter,'Value')+2;
 
    handles.DataProperties(nr).PathName=handles.PathName;
    handles.DataProperties(nr).FileName=handles.FileName;
    handles.DataProperties(nr).CombinedDataset=0;
    
    if handles.AllM==0
        [M1,M2,MStep]=ReadFirstStepLast(get(handles.EditM,'String'));
        handles.DataProperties(nr).M1=M1;
        handles.DataProperties(nr).M2=M2;
        handles.DataProperties(nr).MStep=MStep;
    else
        handles.DataProperties(nr).M1=0;
        handles.DataProperties(nr).M2=0;
        handles.DataProperties(nr).MStep=1;
    end    
    if handles.AllN==0
        [N1,N2,NStep]=ReadFirstStepLast(get(handles.EditN,'String'));
        handles.DataProperties(nr).N1=N1;
        handles.DataProperties(nr).N2=N2;
        handles.DataProperties(nr).NStep=NStep;
    else
        handles.DataProperties(nr).N1=0;
        handles.DataProperties(nr).N2=0;
        handles.DataProperties(nr).NStep=1;
    end    
    
    handles.DataProperties(nr).MultiplyX=1.0;
    handles.DataProperties(nr).AddX=0.0;
        
    handles.DataProperties=ImportUCITxyz(handles.DataProperties,nr);
 
else
    txt='Dataset name already exists!';
    mp_giveWarning('WarningText',txt);
end
 
guidata(hObject, handles);
 
 
function EditDatasetName_Callback(hObject, eventdata, handles)
% hObject    handle to EditDatasetName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: get(hObject,'String') returns contents of EditDatasetName as text
%        str2double(get(hObject,'String')) returns contents of EditDatasetName as a double
 
handles.Name=get(hObject,'String');
 
guidata(hObject, handles);
 
 
% --- Executes during object creation, after setting all properties.
function EditDatasetName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to EditDatasetName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
 
 
function handles=RefreshDatasetName(handles);
 
% i=get(handles.SelectParameter,'Value');
% str=get(handles.SelectParameter,'String');
%  
% j=get(handles.SelectBlock,'Value');
% strbl=get(handles.SelectBlock,'String');
%  
% set(handles.EditDatasetName,'String',[str{i} ' - ' strbl{j}]);
 
 
 
 
 



function EditM_Callback(hObject, eventdata, handles)
% hObject    handle to EditM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of EditM as text
%        str2double(get(hObject,'String')) returns contents of EditM as a double


% --- Executes during object creation, after setting all properties.
function EditM_CreateFcn(hObject, eventdata, handles)
% hObject    handle to EditM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function EditN_Callback(hObject, eventdata, handles)
% hObject    handle to EditN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of EditN as text
%        str2double(get(hObject,'String')) returns contents of EditN as a double


% --- Executes during object creation, after setting all properties.
function EditN_CreateFcn(hObject, eventdata, handles)
% hObject    handle to EditN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in ToggleAllM.
function ToggleAllM_Callback(hObject, eventdata, handles)
% hObject    handle to ToggleAllM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of ToggleAllM

handles.AllM=get(hObject,'Value');
if handles.AllM
    handles.M1=0;
    handles.M2=0;
else
    handles.M1=str2num(get(handles.EditM,'String'));
    handles.M2=handles.M1;
end

handles=RefreshOptions(handles);
% handles=RefreshDatasetName(handles);

guidata(hObject, handles);

% --- Executes on button press in ToggleAllN.
function ToggleAllN_Callback(hObject, eventdata, handles)
% hObject    handle to ToggleAllN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of ToggleAllN

handles.AllN=get(hObject,'Value');
if handles.AllN
    handles.N1=0;
    handles.N2=0;
else
    handles.N1=str2num(get(handles.EditN,'String'));
    handles.N2=handles.N1;
end

handles=RefreshOptions(handles);
% handles=RefreshDatasetName(handles);

guidata(hObject, handles);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function handles=RefreshOptions(handles);

if handles.AllM==1
    set(handles.EditM,'Enable','off','BackGroundColor',[0.831 0.816 0.784]);
else
    set(handles.EditM,'Enable','on','BackGroundColor',[1.000 1.000 1.000]);
end
if handles.AllN==1
    set(handles.EditN,'Enable','off','BackGroundColor',[0.831 0.816 0.784]);
else
    set(handles.EditN,'Enable','on','BackGroundColor',[1.000 1.000 1.000]);
end


