function ui_main(varargin)

%  Initialize and hide the GUI as it is being constructed.
    frameWidth=450;
    frameHeight=450;
    
    frame = figure('Visible','off','Position',[0,0,frameWidth,frameHeight]);
    movegui(frame,'center'); % get this thing visible on smaller displays.
    set(frame,'Name','NODalyze');
    set(frame,'NumberTitle','off');
    set(frame,'MenuBar','none');
    set(frame,'Resize','on');
    set(frame,'Units','normalized');
    set(frame,'Color','y');

    task = struct;

    model = struct;
    
    model.txtBaseDir = createLabel(frame,[0 0.97 0.8 0.03],'');
    set(model.txtBaseDir,'BackgroundColor','w');
    set(model.txtBaseDir,'ForegroundColor','b');
    
    model.txtStudyID = createLabel(frame,[0.8 0.97 0.2 0.03],'');
    set(model.txtStudyID,'BackgroundColor','w');
    set(model.txtStudyID,'ForegroundColor','r');    

    TASK_HEIGHT = 1-0.13;
    
    % PREPROCESSING
    task.preprocessing   = uipanel(frame,'Title','Preprocessing','Position',[0 0.0 1 TASK_HEIGHT]);
    set(task.preprocessing,'BackgroundColor','w');
    set(task.preprocessing,'Units','normalized');
    model = createFirstStepPanel(model,task.preprocessing);
    
    % CLASSIFICATION
    task.classification = uipanel(frame,'Title','Classification','Position',[0 0.0 1 TASK_HEIGHT]);
    set(task.classification,'BackgroundColor','w');
    model = createSecondStepPanel(model,task.classification);
    
    % PLOT
    task.plot = uipanel(frame,'Title','Plot','Position',[0 0.0 1 TASK_HEIGHT]);
    set(task.plot,'BackgroundColor','w');
    model = createVisualStepPanel(model,task.plot);
    
    % TASK SWITCH BUTTONS
    task.taskSwitch = uibuttongroup(frame,'Position',[0 1-0.13 1 0.10]);
    % controlls togglebuttons
        set(task.taskSwitch,'BackgroundColor','w');
        set(task.taskSwitch,'Units','normalized');
    
        btnSwitchPreprocessing = uicontrol(task.taskSwitch,'Style','pushbutton',...
            'String','Image Processing',...
            'Units','normalized','Position',[0.0 0.0 0.33 1]);
        set(btnSwitchPreprocessing,'Callback',{@cbSwitchTask,'PRE',task}); 
        set(btnSwitchPreprocessing,'Enable','on');

        btnSwitchClassify = uicontrol(task.taskSwitch,'Style','pushbutton',...
            'String','Decode',...
            'Units','normalized','Position',[0.33 0.0 0.33 1]);
        set(btnSwitchClassify,'Callback',{@cbSwitchTask,'CLASSIFY',task}); 
        set(btnSwitchClassify,'Enable','on');

        btnSwitchPlot = uicontrol(task.taskSwitch,'Style','pushbutton',...
            'String','Plot',...
            'Units','normalized','Position',[0.66 0.0 0.33 1]);
        set(btnSwitchPlot,'Callback',{@cbSwitchTask,'PLOT',task}); 
        set(btnSwitchPlot,'Enable','on');


    % MENUS

    savemenu = uimenu(frame,'Label','Save ...','Enable','on');
        uimenu(savemenu,'Label','Save Parameter','Callback',{@mcb_save,'PARAMS',model},'Enable','on');
        uimenu(savemenu,'Label','Save ImageProcesseding Data','Callback',{@mcb_save,'IMAGEPROCESSING',model},'Enable','on');
        uimenu(savemenu,'Label','Load ImageProcesseding Data','Callback',{@mcb_load,'IMAGEPROCESSING',model},'Enable','on');
        uimenu(savemenu,'Label','Save Decode Data','Callback',{@mcb_save,'DECODE',model},'Enable','on');
        uimenu(savemenu,'Label','Load Decode Data','Callback',{@mcb_load,'DECODE',model},'Enable','on');
        
    studySelectMenu = uimenu(frame,'Label','Study ...','Enable','on');
        uimenu(studySelectMenu,'Label','*new Study*','Callback',{@mcb_new_study,studySelectMenu,model});
        fillStudyMenu(studySelectMenu,model);
        
    updateMenu = uimenu(frame,'Label','Update ...','Enable','on');
        uimenu(updateMenu,'Label','rescan subject dir','Callback',{@mcb_update_subjects,model});
        uimenu(updateMenu,'Label','rescan images','Callback',{@mcb_update_imagebase,model});
        
%     newStudyMenu = uimenu(frame,'Label','new Study','Enable','on');

    load_study([getPreviousStudyID '.mat'],model);
    cbSwitchTask(0,0,'PRE',task);
    set(frame,'Visible','on');

end

%%%%% ui elements
function model = createFirstStepPanel(model,parent)
    norm1Model = {'none','mean','minmax'};
    roiGroupMethodNames = {'none','mean','max','median'};
    roiGroupMethodFunctions = {@(in)in, @(in)nanmean(in),@(in)nanmax(in),@(in)nanmedian(in)};

    main_grid = cell(2,4);
    main_grid{1,1} = [0 0.7 0.4 0.3];
    main_grid{1,2} = [0 0.5 0.5 0.2];
    main_grid{1,3} = [0 0.1 0.5 0.4];
    main_grid{1,4} = [0 0.0 1.0 0.1];
    
    main_grid{2,1} = [0.4 0.7 0.6 0.3];
    main_grid{2,2} = [0.5 0.1 0.5 0.6];
%     main_grid{2,3} = [0.5 0.1 0.5 0.4];
    
    %Subjects
    pSubject = uipanel(parent,'Units','normalized','Position',cell2mat(main_grid(1,1)));
    set(pSubject,'Title','Subjects');
    set(pSubject,'BackgroundColor','w');
    
%     subjectNames = listDirNames(getBaseDir(model));
    model.subjectSelector = uicontrol(pSubject,'Style','listbox',...
                    'Min',1, 'Max',3,...
                    'Units','normalized',...
                    'Position',[0 0 1 1]);
    set(model.subjectSelector, 'FontName', 'FixedWidth');
    set(model.subjectSelector,'BackgroundColor','w');

  
    %Classes
    pClasses = uipanel(parent,'Units','normalized','Position',cell2mat(main_grid(1,2)));
    set(pClasses,'Title','Class Definitions');
    set(pClasses,'BackgroundColor','w');
        lClassDef = uicontrol(pClasses,...
            'Style','text',...
            'String',sprintf('<Label>,\t <[Event, Event, ..]>;'),...
            'Units','normalized',...
            'Position',[0 0.8 1 0.2]);
        set(lClassDef,'BackgroundColor','w');
        set(lClassDef,'HorizontalAlignment','left');

        
        model.txtClassDef = uicontrol(pClasses,'Style','edit',...
            'Units','normalized',...
            'Position',[0 0 1 0.8]);
        set(model.txtClassDef,'HorizontalAlignment','left');
        set(model.txtClassDef,'Max',20);
        set(model.txtClassDef,'Min',0);
        set(model.txtClassDef, 'FontName', 'FixedWidth');
        set(model.txtClassDef, 'BackgroundColor', 'w');


    %Timeline
    pPSTH        = uipanel(parent,'Title','PST Options','Position',cell2mat(main_grid(2,1)));
    set(pPSTH,'BackgroundColor','w');
        
        grid_h = 0.16;
        grid_w = 0.3;
        
        tl_grid = cell([3 6]);
        tl_grid{2,1} = [0.0 0.83 grid_w grid_h];
        tl_grid{2,1} = [0.4 0.83 grid_w grid_h];
        tl_grid{3,1} = [0.7 0.83 grid_w grid_h];
        
        tl_grid{1,2} = [0.0 0.66 grid_w grid_h];
        tl_grid{2,2} = [0.4 0.66 grid_w grid_h];
        tl_grid{3,2} = [0.7 0.66 grid_w grid_h];

        tl_grid{1,3} = [0.0 0.5 grid_w grid_h];
        tl_grid{2,3} = [0.4 0.5 grid_w grid_h];
        tl_grid{3,3} = [0.7 0.5 grid_w grid_h];
        
        tl_grid{1,4} = [0.0 0.33 grid_w grid_h];
        tl_grid{2,4} = [0.4 0.33 grid_w grid_h];
        tl_grid{3,4} = [0.7 0.33 grid_w grid_h];

        tl_grid{1,5} = [0.0 0.16 grid_w grid_h];
        tl_grid{2,5} = [0.4 0.16 grid_w grid_h];
        tl_grid{3,5} = [0.7 0.16 grid_w grid_h];
        
        tl_grid{1,6} = [0.0 0.0 0.5 grid_h];
        tl_grid{2,6} = [0.5 0.0 grid_w grid_h];
        tl_grid{3,6} = [0.75 0.0 grid_w grid_h];

        lStart      = createLabel(pPSTH, cell2mat(tl_grid(2,1)) ,'Start [sec]');
        lEnd        = createLabel(pPSTH, cell2mat(tl_grid(3,1)) ,'End [sec]');
        lPSTH       = createLabel(pPSTH, cell2mat(tl_grid(1,2)) ,'PST Range');
        lBaseline   = createLabel(pPSTH, cell2mat(tl_grid(1,3)) ,'Baseline');
        lTRF        = createLabel(pPSTH, cell2mat(tl_grid(1,5)) ,'TR Factor');
        lTRSec      = createLabel(pPSTH, cell2mat(tl_grid(3,4)) ,'TR [sec]');

        model.txtPSTHStart         = createTextField(pPSTH,cell2mat(tl_grid(2,2)),'');
        model.txtPSTHEnd           = createTextField(pPSTH,cell2mat(tl_grid(3,2)),'');
        model.txtBaselineStart     = createTextField(pPSTH,cell2mat(tl_grid(2,3)),'');
        model.txtBaselineEnd       = createTextField(pPSTH,cell2mat(tl_grid(3,3)),'');
        model.txtTrFactor          = createTextField(pPSTH,cell2mat(tl_grid(2,5)),'');
        model.labelTR              = createTextField(pPSTH,cell2mat(tl_grid(3,5)),'');
        
        % OPTIONS
        grid_h = 0.16;
        grid_wl = 0.4;
        grid_wo = 0.6;
        
        optGrid = cell([3 6]);
        optGrid{1,1} = [0.0 0.83 grid_wl grid_h];
        optGrid{2,1} = [0.4 0.83 grid_wo grid_h];
        
        optGrid{1,2} = [0.0 0.66 grid_wl grid_h];
        optGrid{2,2} = [0.4 0.66 grid_wo grid_h];

        optGrid{1,3} = [0.0 0.5 1.0 grid_h];
%         optGrid{2,3} = [0.4 0.5 grid_wo grid_h];
        
        optGrid{1,4} = [0.0 0.33 grid_wl grid_h];
        optGrid{2,4} = [0.4 0.33 grid_wo grid_h];

        optGrid{1,5} = [0.0 0.16 1.0 grid_h];
%         optGrid{2,5} = [0.4 0.16 grid_wo grid_h];
        
        optGrid{1,6} = [0.0 0.0 grid_wl grid_h];
        optGrid{2,6} = [0.4 0.0 grid_wo grid_h];

        % Options:Imagebase
        pOptions = uipanel(parent,'Title','Options','Position',cell2mat(main_grid(2,2)));
        set(pOptions,'BackgroundColor','w');

        createLabel(pOptions,cell2mat(optGrid(1,1)),'Image Base');
        model.imageTypeSelection = uicontrol(pOptions,'Style','popupmenu',...
        'Units','normalized',...
        'Position',cell2mat(optGrid(2,1)));
        set(model.imageTypeSelection,'BackgroundColor','w');
        
        %Options:normalizations
        
        createLabel(pOptions,cell2mat(optGrid(1,2)),'PST normalization');
        model.selNormPST = uicontrol(pOptions,'Style','popupmenu',...
            'Units','normalized',...
            'Position',cell2mat(optGrid(2,2)),...
            'String',norm1Model,...
            'UserData',norm1Model);
         set(model.selNormPST,'BackgroundColor','w');   
        
        model.chkColBias = uicontrol(pOptions,'Style','checkbox',...
            'Units','normalized',...
            'Position',cell2mat(optGrid(1,3)),...
            'String','Remove Column Bias within class',...
            'Enable','on');
         set(model.chkColBias,'BackgroundColor','w');   
         
         %Options:spatialGrouping

         createLabel(pOptions,cell2mat(optGrid(1,4)),'Spatial Grouping');
         model.selRoiGrouping = uicontrol(pOptions,'Style','popupmenu',...
                'Units','normalized',...
                'Position',cell2mat(optGrid(2,4)),...
                'String',roiGroupMethodNames,...
                'UserData',roiGroupMethodFunctions);
         set(model.selRoiGrouping,'BackgroundColor','w');
         
         %Options:VOI selection for FBS
         btnRunButton2 = uicontrol(pOptions,'String',sprintf('load VOI for Searchlight'),...
             'Units','normalized','Position',cell2mat(optGrid(1,5)));
         set(btnRunButton2,'Enable','on');
 
        
        % COORD TABLE
        pVoxel = uipanel(parent,'Title','Select ROIs','Position',cell2mat(main_grid(1,3)));
        set(pVoxel,'BackgroundColor','w');
        lVoxelDef = createLabel(pVoxel, [0 0.9 1 0.1],'<ROI Name> [+ radius (mm)];');
        model.txtVoxelDef = createTextField(pVoxel,[0 0.15 1 0.75],'');
        set(model.txtVoxelDef,'HorizontalAlignment','left');
        set(model.txtVoxelDef,'Max',20);
        set(model.txtVoxelDef,'Min',0);
        set(model.txtVoxelDef, 'FontName', 'FixedWidth');
        
        btnROIlist = uicontrol(pVoxel,'String','show available ROIs','Units','normalized',...
            'Position',[0 0 1 0.15]);
        set(btnROIlist,'Enable','on');
        
        %buttons
        pButtons = uipanel(parent,'Position',cell2mat(main_grid(1,4)));
        set(pButtons,'BackgroundColor','w');
        
        btnRunButton1 = uicontrol(pButtons,'String','start ROI-List processing',...
            'Units','normalized','Position',[0 0 0.33 1]);
        set(btnRunButton1,'Enable','on');

        btnRunButton3 = uicontrol(pButtons,'String','start ROI-Image processing',...
            'Units','normalized','Position',[0.66 0 0.33 1]);
        set(btnRunButton3,'Enable','on');
        
        
        set(btnRunButton1,'Callback',{@cbRunPreprocessing,model,'COORD'}); % set here, because of model.
        set(btnRunButton2,'Callback',{@cbRunPreprocessing,model,'FBS'});   % set here, because of model.  
        set(btnRunButton3,'Callback',{@cbRunPreprocessing,model,'ROI'});   % set here, because of model.    
        set(btnROIlist,   'Callback',{@cbShowAvailableROIs,model});        % set here, because of model.  
   
end

function model = createSecondStepPanel(model,parent)
basecolor = 'w';

TimeframeGroupingStrings = {'none','mean'};%,'sum,'max','median'};
TimeframeGroupingFunctions = {@(in)in,@(in)nanmean(in,2)};%,'sum',','max','median'};

pTime = uipanel(parent,'Units','normalized','Position',[0.0 0.7 1 0.3]);
    set(pTime,'Title','Decode Timerange Options');
    set(pTime,'BackgroundColor',basecolor);
    grid_h = 0.25;
    grid_w = 0.3;
    time_grid = cell([3 4]);
    time_grid{1,1} = [0.0 0.75 grid_w grid_h];
    time_grid{2,1} = [0.4 0.75 grid_w grid_h];
    time_grid{3,1} = [0.7 0.75 grid_w grid_h];
    time_grid{1,2} = [0.0 0.50 grid_w grid_h];
    time_grid{2,2} = [0.4 0.50 grid_w grid_h];
    time_grid{3,2} = [0.7 0.50 grid_w grid_h];
    time_grid{1,3} = [0.0 0.25 grid_w grid_h];
    time_grid{2,3} = [0.4 0.25 grid_w grid_h];
    time_grid{3,3} = [0.7 0.0 grid_w grid_h];
    time_grid{1,4} = [0.0 0.0 grid_w grid_h];
    time_grid{2,4} = [0.4 0.0 grid_w grid_h];
    time_grid{3,4} = [0.7 0.0 grid_w grid_h];
    
    lStart      = createLabel(pTime, cell2mat(time_grid(2,1)) ,'Start [sec]');
    lEnd        = createLabel(pTime, cell2mat(time_grid(3,1)) ,'End [sec]');
    lFrameShift = createLabel(pTime, cell2mat(time_grid(1,2)),'Time Range');
    lFramsSize  = createLabel(pTime, cell2mat(time_grid(1,3)),'Shifting Time Frame [sec]');
    lFramegroup = createLabel(pTime, cell2mat(time_grid(1,4)),'Time Frame Grouping');
    
    model.txtFrameShiftStart   = createTextField(pTime,cell2mat(time_grid(2,2)),'');
    model.txtFrameShiftEnd     = createTextField(pTime,cell2mat(time_grid(3,2)),'');
    model.txtFrameShiftDur     = createTextField(pTime,cell2mat(time_grid(2,3)),'');
    

    
    model.selTimeframeGrouping = uicontrol(pTime,'Style','popupmenu',...
        'Units','normalized',...
        'Position',cell2mat(time_grid(2,4)),...
        'String',TimeframeGroupingStrings,...
        'UserData',TimeframeGroupingFunctions);
    set(model.selTimeframeGrouping,'Enable','on');
    
pSVM = uipanel(parent,'Units','normalized','Position',[0 0.0 0.499 0.7]);
    set(pSVM,'Title','SVM Classification (libSVM)');
    set(pSVM,'BackgroundColor',basecolor);

    grid_h = 0.125;

    svmgrid = cell([2 8]);
    svmgrid{1,1} = [0.0 0.875 1.0 grid_h];
    svmgrid{1,2} = [0.0 0.750 0.5 grid_h];
    svmgrid{2,2} = [0.5 0.750 0.5 grid_h];
    svmgrid{1,3} = [0.0 0.625 1   grid_h];
    svmgrid{1,4} = [0.0 0.5   1   grid_h];
    svmgrid{1,5} = [0.0 0.375 1   grid_h];
    svmgrid{1,6} = [0.0 0.250 0.5 grid_h];
    svmgrid{2,6} = [0.5 0.250 0.5 grid_h];
    svmgrid{1,7} = [0.0 0.125 0.5 grid_h];
    svmgrid{2,7} = [0.5 0.125 0.5 grid_h];
    svmgrid{1,8} = [0.0 0.0   1   grid_h];
    
    
    model.txtSVMopts = createTextField(pSVM,cell2mat(svmgrid(1,1)),'');
    set(model.txtSVMopts,'HorizontalAlignment','left');
    
    model.txtSVMnfold = createTextField(pSVM,cell2mat(svmgrid(1,2)),'');
    createLabel(pSVM,cell2mat(svmgrid(2,2)),'-Fold CrossVal');
    
    btnRunSVM = uicontrol(pSVM,'String','run SVM within-Subject crossvalidation',...
        'Units','normalized',...
        'Position',cell2mat(svmgrid(1,3)));
    set(btnRunSVM,'Enable','on');
    
    btnRunXSVM = uicontrol(pSVM,'String','run SVM across-Subject crossvalidation',...
        'Units','normalized',...
        'Position',cell2mat(svmgrid(1,4)));
    set(btnRunXSVM,'Enable','on');
    
    lSearchligh = createLabel(pSVM, cell2mat(svmgrid(1,6)),'Searchlight Radius (mm)');
    model.txtSearchlightRadius = createTextField(pSVM,cell2mat(svmgrid(2,6)),'');
    
    lFBSTiming = createLabel(pSVM, cell2mat(svmgrid(1,7)),'Timepoints [sec] (opt.)');
    set(lFBSTiming,'TooltipString','e.g. ''-5 0 5 10 15''');
    model.txtSearchlightTimeline = createTextField(pSVM,cell2mat(svmgrid(2,7)),'');

    btnRunFBS = uicontrol(pSVM,'String','run Searchlight',...
        'Units','normalized',...
        'Position',cell2mat(svmgrid(1,8)));
    set(btnRunFBS,'Enable','on');
    
    
pSOM = uipanel(parent,'Units','normalized','Position',[0.5 0.3 0.499 0.4]);
    set(pSOM,'Title','SOM Classification (somtoolbox2)');
    set(pSOM,'BackgroundColor',basecolor);

    createLabel(pSOM,[0 0.75 0.3 0.20],'Size:');
    model.txtSomM = createTextField(pSOM,[0.3 0.75 0.25 0.16],'');
    somsizemal = createLabel(pSOM,[0.55 0.75 0.2 0.16],'x');
    set(somsizemal,'HorizontalAlignment','center');
    model.txtSomN = createTextField(pSOM,[0.75 0.75 0.25 0.16],'');
    latticeModel = {'rect','hexa'};
    model.selSomLattice = uicontrol(pSOM,'Style','popupmenu',...
        'Units','normalized',...
        'Position',[0.0 0.5 0.3 0.16],...
        'String',latticeModel,...
        'UserData',latticeModel);
    set(model.selSomLattice,'BackgroundColor','w');

    model.txtSOMnfold = createTextField(pSOM,[0.5 0.50 0.25 0.16],'');
    createLabel(pSOM,[0.75 0.5 0.25 0.16 ],'-Fold CrossVal');

    btnRunSOM = uicontrol(pSOM,'String','run SOM within-Subject crossvalidation',...
        'Units','normalized',...
        'Position',[0.0 0.25 1 0.25]);
    set(btnRunSOM,'Enable','on');

    btnRunXSOM = uicontrol(pSOM,'String','run SOM across-Subject crossvalidation',...
        'Units','normalized',...
        'Position',[0.0 0.0 1 0.25]);
    set(btnRunXSOM,'Enable','on');
    
% OPTIONS
pOptions = uipanel(parent,'Units','normalized','Position',[0.5 0 0.5 0.3]);
    set(pOptions,'Title','Other Options');
    set(pOptions,'BackgroundColor',basecolor);
    
    model.chkSVMrnd = uicontrol(pOptions,'Style','checkbox','Units','normalized','Position',[0 0.66 1 0.33]);
    set(model.chkSVMrnd,'String','Randomize Trial Order');
    set(model.chkSVMrnd,'BackgroundColor','w');


% button callbacks set here, because of model.
    set(btnRunSVM, 'Callback',{@cbRunDecode,model,'SVM'}); 
    set(btnRunXSVM,'Callback',{@cbRunDecode,model,'XSVM'}); 
    set(btnRunSOM, 'Callback',{@cbRunDecode,model,'SOM'});
    set(btnRunXSOM,'Callback',{@cbRunDecode,model,'XSOM'});
    set(btnRunFBS, 'Callback',{@cbRunDecode,model,'FBS'});
end

function model = createVisualStepPanel(model,parent)

    grid_h = 0.25;
    grid_w = 0.5;
    plot_grid = cell([3 3]);
    plot_grid{1,1} = [0.0 0.63 grid_w grid_h];
    plot_grid{1,2} = [0.0 0.33 grid_w grid_h];
    plot_grid{1,3} = [0.0 0.03 grid_w grid_h];
    plot_grid{2,1} = [0.25 0.63 grid_w grid_h];
    plot_grid{2,2} = [0.25 0.33 grid_w grid_h];
    plot_grid{2,3} = [0.25 0.03 grid_w grid_h];

    pButtonPane = uipanel(parent,'Units','normalized','Position',[0 0.5 1 0.5]);
%     set(pButtonPane,'Title','Plot');
    set(pButtonPane,'BackgroundColor','w');
    
    btnPlot03 = uicontrol(pButtonPane,'String','Plot PSTH',...
        'Units','normalized',...
        'Position',cell2mat(plot_grid(2,1)));
    set(btnPlot03,'Callback',{@cbPlot,model,'psth'}); 
    set(btnPlot03,'Enable','on');
  
    btnPlot01 = uicontrol(pButtonPane,'String','Plot Decode Performance +/- SE',...
        'Units','normalized',...
        'Position',cell2mat(plot_grid(2,2)));
    set(btnPlot01,'Callback',{@cbPlot,model,'simple'});
    set(btnPlot01,'Enable','on');
    
    btnPlot02 = uicontrol(pButtonPane,'String','Plot Individual Performance and Mean +/- SE',...
        'Units','normalized',...
        'Position',cell2mat(plot_grid(2,3)));
    set(btnPlot02,'Callback',{@cbPlot,model,'x-subject-val'});
    set(btnPlot02,'Enable','on');

    
end

%%%%% ui callbacks

function cbShowAvailableROIs(src,evtl,model)
    ui_showAvailableROIs(model);
end

function cbSwitchTask(src,evnt,task,taskpanel)
set(taskpanel.preprocessing,'Visible','off');
set(taskpanel.classification,'Visible','off');
set(taskpanel.plot,'Visible','off');
switch task
    case 'PRE'
        set(taskpanel.preprocessing,'Visible','on');

    case 'CLASSIFY'
        set(taskpanel.classification,'Visible','on');

    case 'PLOT'
        set(taskpanel.plot,'Visible','on');
end
% assignin('base','model',model);
end

function cbRunPreprocessing(src,evnt,model,task)
main(model,'pre',task);
end

function cbRunDecode(src,evnt,model,task)
main(model,'decode',task);
end

function cbPlot(src,evnt,model,type)
main(model,'plot',type);
end

%%%%%%%%%% menu callbacks

function mcb_save(src,evnt,token,model)
switch token
    case 'PARAMS'
        studyID = get(model.txtStudyID,'String');
        saveStudy(studyID,model);
    case 'IMAGEPROCESSING'
        disp('save img data..');
        [FileName,PathName,FilterIndex] = uiputfile('*.mat','Save ImageProcessing Data') ;
        if(~strcmp(FileName,''))
            evalin('base', sprintf('save(''%s'',''%s'')',fullfile(PathName,FileName),'preprocessedData'));
            disp('saved');
        end
%         [FileName,PathName,FilterIndex] = uigetfile('*.mat','Load ImageProcessing Data') 
%         evalin('base', sprintf('load(%s,%s)',fullfile(PathName,FileName),'preprocessedData'));
    case 'DECODE'
        disp('save decode data..');
        [FileName,PathName,FilterIndex] = uiputfile('*.mat','Save Decode Data') ;
        if(~strcmp(FileName,''))
            evalin('base', sprintf('save(''%s'',''%s'')',fullfile(PathName,FileName),'decode'));
            disp('saved');
        end
end
end

function mcb_load(src,evnt,token,model)
switch token
    case 'IMAGEPROCESSING'
        disp('load img data..');
        [FileName,PathName,FilterIndex] = uigetfile('*.mat','Load ImageProcessing Data') ;
        if(~strcmp(FileName,''))
            evalin('base', sprintf('load(''%s'',''%s'')',fullfile(PathName,FileName),'preprocessedData'));
            disp('done');
        end
    case 'DECODE'
        disp('load decode data..');
        [FileName,PathName,FilterIndex] = uigetfile('*.mat','Load Decode Data') ;
        if(~strcmp(FileName,''))
            evalin('base', sprintf('load(''%s'',''%s'')',fullfile(PathName,FileName),'decode'));
            disp('done');
        end
end
end

function mcb_update_subjects(src,evnt,model)
studyID = get(model.txtStudyID,'String');
baseDir = getBaseDir(model);
subjectNames = listDirNames(baseDir);

set(model.subjectSelector,'String',subjectNames);
set(model.subjectSelector,'UserData',subjectNames);
set(model.subjectSelector,'Value',1);

saveStudy(studyID,model);
end

function mcb_update_imagebase(src,evnt,model)
studyID = get(model.txtStudyID,'String');

subjectNames = getSubjectCellList(model);
imageDir = fullfile(getBaseDir(model),cell2mat(subjectNames(1)));
imageMask = imageMaskNames(imageDir);

set(model.imageTypeSelection,'String',imageMask);
set(model.imageTypeSelection,'UserData',imageMask);
set(model.imageTypeSelection,'Value',1);

saveStudy(studyID,model);
end


function mcb_new_study(src,evnt,studymenu,uimodel)
studyID = get(uimodel.txtStudyID,'String');
saveStudy(studyID,uimodel);

display('CREATE STUDY');
    ui_createStudy(studymenu,uimodel);
end