function varargout = pathchooser(varargin)
% PATHCHOOSER M-file for pathchooser.fig
%      PATHCHOOSER, by itself, creates a new PATHCHOOSER or raises the existing
%      singleton*.
%
%      H = PATHCHOOSER returns the handle to a new PATHCHOOSER or the handle to
%      the existing singleton*.
%
%      PATHCHOOSER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PATHCHOOSER.M with the given input arguments.
%
%      PATHCHOOSER('Property','Value',...) creates a new PATHCHOOSER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before pathchooser_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to pathchooser_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 pathchooser

% Last Modified by GUIDE v2.5 18-Jan-2013 13:54:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pathchooser_OpeningFcn, ...
                   'gui_OutputFcn',  @pathchooser_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
% End initialization code - DO NOT EDIT

% func. to switch selected entries
function switchEntries(lb_source, lb_dest)
content = get(lb_source, 'String');
inds = get(lb_source, 'Value');
if ~isempty(content)
    old = get(lb_dest, 'String');
    new = [old; content(inds)];
    set(lb_dest,'Value',[]);
    set(lb_dest,'String',new);
    content(inds) = [];
    set(lb_source,'Value',[]);
    set(lb_source,'String',content);
end

function [list inds] = upEntries(list,inds)
if inds(1)>1
    for i=inds
        MoveDown=list(i-1);
        list(i-1)=list(i);
        list(i)=MoveDown;
    end
    inds = inds -1;
end

function [list inds] = downEntries(list,inds)
if inds(end)<size(list,1)
    for i=fliplr(inds)
        MoveDown=list(i+1);
        list(i+1)=list(i);
        list(i)=MoveDown;
    end
    inds = inds+1;
end

% --- Executes just before pathchooser is made visible.
function pathchooser_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 pathchooser (see VARARGIN)

% Choose default command line output for pathchooser


%handles.output = hObject;

% Update handles structure
%guidata(hObject, handles);

% UIWAIT makes pathchooser wait for user response (see UIRESUME)
tmpdir =  varargin{1};
ud.baseDir = tmpdir{:};
ud.modus = varargin{2};
set(handles.pathchooser,'UserData',ud);
ud = get(handles.pathchooser,'UserData');
ud.pattern = cellstr([repmat('class ',26,1), num2str([1:26]')]);
ud.folders = cell(1,26);
baseDir = ud.baseDir;
dirContent = dir(baseDir);
contentNames = {dirContent.name};
if strcmp(ud.modus,'dirs')
    contentNames = contentNames(vertcat(dirContent.isdir));
elseif strcmp(ud.modus,'files')
    pat = {'.*.(img|nii|gz)'};
    content = regexp(contentNames,pat,'match');
    imgIdx = ~cellfun(@isempty,content);
    contentNames = vertcat(content{imgIdx});
end
set(handles.listbox4,'String', contentNames);
set(handles.pathchooser,'UserData', ud);
if size(varargin,2) > 3
    ud.pattern(1:length(varargin{3})) = varargin{4};
    ud.folders(1:length(varargin{3})) = varargin{3};
    set(handles.pathchooser,'UserData', ud);
    set(handles.slider1,'Value',length(varargin{3}));
    slider1_Callback(hObject, eventdata, handles);
    pulldown_c1_Callback(hObject, eventdata, handles);
    pulldown_c2_Callback(hObject, eventdata, handles);
end
% if no reset was made
if size(varargin,2) ~= 3
    uiwait(handles.pathchooser);
end
updateMemberCount(handles);

% --- Outputs from this function are returned to the command line.
function varargout = pathchooser_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;
%global baseDir;
try
ud = get(handles.pathchooser,'UserData');
baseDir = ud.baseDir;
classCount = get(handles.slider1,'Value');
subjects = {ud.folders{1:classCount}};
classNames = ud.pattern(1:classCount);
out{1} = subjects;
out{2} = classNames;
out{3} = baseDir;
out{4} = ud.modus;
out{5} = handles;
handles.output = out;
varargout{1} = handles.output;
catch
    handles.output = 0;
    varargout{1} = handles.output;
end


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1


% --- Executes during object creation, after setting all properties.
function listbox4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (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.
%global baseDir;
%baseDir = uigetdir(pwd, 'Select Basedirectory of study');

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


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
switchEntries(handles.listbox4, handles.listbox5);
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updateUserData(handles,2);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
switchEntries(handles.listbox5, handles.listbox4);
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updateUserData(handles,2);

% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox2 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox3.
function listbox3_Callback(hObject, eventdata, handles)
% hObject    handle to listbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox3


% --- Executes during object creation, after setting all properties.
function listbox3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox3 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox4.
function listbox4_Callback(hObject, eventdata, handles)
% hObject    handle to listbox4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox4 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox4


% --- Executes during object creation, after setting all properties.
function listbox5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox4 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox5.
function listbox5_Callback(hObject, eventdata, handles)
% hObject    handle to listbox5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox5 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox5


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox5 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
switchEntries(handles.listbox3, handles.listbox4);
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updateUserData(handles,1);

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
switchEntries(handles.listbox4, handles.listbox3);
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
updateUserData(handles,1);

% --- Executes on key press with focus on pushbutton4 and none of its controls.
function pushbutton4_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
%global baseDir;
ud = get(handles.pathchooser,'UserData');
baseDir = ud.baseDir;
classCount = get(handles.slider1,'Value');
subjects = {ud.folders{1:classCount}};
classNames = ud.pattern(1:classCount);
out{1} = subjects; %get(handles.listbox3, 'String');
out{2} = classNames; %get(handles.listbox5, 'String');
out{3} = baseDir;
out{4} = ud.modus;
out{5} = handles;
if size(unique(cellfun(@(x) size(x,1),out{1})),2) > 1
    answer = questdlg(['The classes are unbalanced! This means the amount', ...
    ' of subjects in class 1 is not equal to the amount of class2. This could ', ...
    ' lead to unfeasible classifications. Nevertheless it could make sense, if you', ...
    ' aware of this fact and you know what you are doing. Do you want to continue?'], ...
    'Attention!', 'Yes', 'No','No');
    if strcmp(answer,'No')
        return
    end
end
if ~isempty(find((cellfun(@(x) size(x,1),out{1}))==0))
    helpdlg('You should choose some subjects, otherwise it will not work.','No subjects selected');
    return
end
handles.output = out;
pathchooser_OutputFcn(hObject, eventdata, handles);
uiresume;
return
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
funPath = mfilename('fullpath');
helpFile = [funPath(1:end - size(mfilename,2)), 'help.html'];
web(helpFile);

% --- Executes on button press in pushbuttonReset.
function pushbuttonReset_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonReset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%global baseDir;
%baseDir = uigetdir(pwd, 'Select Basedirectory of study');
ud = get(handles.pathchooser,'UserData');
oldBaseDir = ud.baseDir;
baseDir = {uigetdir(oldBaseDir, 'Select New Directory')};
set(handles.listbox3,'String', []);
set(handles.listbox5,'String', []);
pathchooser_OpeningFcn(hObject, eventdata, handles, baseDir, ud.modus,'reset');

% --- Executes on button press in pushbuttonQuit.
function pushbuttonQuit_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonQuit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%try
%figure1_CloseRequestFcn(handles.pathchooser, eventdata, handles)
%catch
close;
%end
return


% --- Executes when user attempts to close pathchooser.
function pathchooser_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to pathchooser (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
delete(hObject);


% --- Executes on button press in up1.
function up1_Callback(hObject, eventdata, handles)
% hObject    handle to up1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
vals = get(handles.listbox3,'Value');
list = get(handles.listbox3,'String');
if ~isempty(list) && ~isempty(vals)
[list vals] = upEntries(list, vals);
set(handles.listbox3,'String',list);
set(handles.listbox3,'Value', vals);
end
updateUserData(handles,1);


% --- Executes on button press in down1.
function down1_Callback(hObject, eventdata, handles)
% hObject    handle to down1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
vals = get(handles.listbox3,'Value');
list = get(handles.listbox3,'String');
if ~isempty(list) && ~isempty(vals)
[list vals] = downEntries(list, vals);
set(handles.listbox3,'String',list);
set(handles.listbox3,'Value', vals);
end
updateUserData(handles,1);

% --- Executes on button press in up2.
function up2_Callback(hObject, eventdata, handles)
% hObject    handle to up2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
vals = get(handles.listbox5,'Value');
list = get(handles.listbox5,'String');
if ~isempty(list) && ~isempty(vals)
[list vals] = upEntries(list, vals);
set(handles.listbox5,'String',list);
set(handles.listbox5,'Value', vals);
end
updateUserData(handles,2);

% --- Executes on button press in down2.
function down2_Callback(hObject, eventdata, handles)
% hObject    handle to down2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
vals = get(handles.listbox5,'Value');
list = get(handles.listbox5,'String');
if ~isempty(list) && ~isempty(vals)
[list vals] = downEntries(list, vals);
set(handles.listbox5,'String',list);
set(handles.listbox5,'Value', vals);
end
updateUserData(handles,2);


% --- Executes on button press in selected.
function selected_Callback(hObject, eventdata, handles)
% hObject    handle to selected (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.pushbutton1,'Enable', 'off');
set(handles.pushbutton2,'Enable', 'off');
set(handles.pushbutton3,'Enable', 'off');
set(handles.pushbutton4,'Enable', 'off');
set(handles.selected,'Enable', 'off');
set(handles.pushbutton5,'Enable', 'on');
set(handles.up1,'Enable', 'on');
set(handles.up2,'Enable', 'on');
set(handles.down1,'Enable', 'on');
set(handles.down2,'Enable', 'on');


% --- Executes on button press in pushbutton_xls.
function pushbutton_xls_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_xls (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 ud = get(handles.pathchooser,'UserData');
 [filename, pathname, filterindex] = uigetfile({'*.xls;*.csv'}, 'Pick an Excel or CSV File');
 if strcmp(filename(end-2:end),'csv')
    data = textread([pathname filename],'%s','delimiter','\n');
    lines = cellfun(@(x) strsplit(',',x),data,'UniformOutput',false);
    classNames =  vertcat(lines{1})';
    cols = vertcat(lines{2:end});
    for k=1:size(cols,2)
       classes{k} = cols(~cellfun(@isempty,cols(:,k)),k);
    end
 elseif strcmp(filename(end-2:end),'xls')
    [a b] = xlsread([pathname filename]);
    classNames = b(1,:)';
    for k=1:size(b,2)
        classes{k} = b(2:end,k);
    end
 else
     error('Unknown Fileformat');
 end
 ud.pattern(1:length(classNames)) = classNames;
 ud.folders(1:length(classes)) = classes;
 set(handles.pathchooser,'UserData', ud);
 set(handles.slider1,'Value',length(classes));
 slider1_Callback(hObject, eventdata, handles);
 pulldown_c1_Callback(hObject, eventdata, handles);
 updateUserData(handles,3);




% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)
% hObject    handle to save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ud = get(handles.pathchooser,'UserData');
ccount = get(handles.slider1,'Value');
members = ud.folders(1:ccount);
[filename, pathname] = uiputfile('*.csv', 'Save Groups as csv');
fn = [pathname filename];
fid = fopen(fn,'wt');
k = repmat({''},max(cellfun(@(x) size(x,1),members))+1,length(members));
k(1,:) = ud.pattern(1:length(members))';
for counter=1:length(members)
  k(2:length(members{counter})+1,counter) = members{counter};
end
try
    fopen(fn,'wt');
    for c=1:size(k,1)
        fprintf(fid, '%s\n',[sprintf('%s,',k{c,1:end-1}),k{c,end}]);
    end
    fclose(fid);
catch ex
    fclose(fid);
    error('Could not write file');
end


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ud = get(handles.pathchooser,'UserData');
set(handles.edit_classCount,'String',num2str(round(get(handles.slider1,'Value'))));
set(handles.pulldown_c1,'Value',min(get(handles.slider1,'Value'),get(handles.pulldown_c1,'Value')));
set(handles.pulldown_c2,'Value',min(get(handles.slider1,'Value'),get(handles.pulldown_c2,'Value')));
set(handles.pulldown_c1,'String',ud.pattern(1:get(handles.slider1,'Value'),:));
set(handles.pulldown_c2,'String',ud.pattern(1:get(handles.slider1,'Value'),:));
pulldown_c1_Callback(hObject, eventdata, handles)
pulldown_c2_Callback(hObject, eventdata, handles)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on selection change in pulldown_c2.
function pulldown_c2_Callback(hObject, eventdata, handles)
% hObject    handle to pulldown_c2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ud = get(handles.pathchooser,'UserData');
currentVal = get(handles.pulldown_c2,'Value');
currentStr = ud.folders{1,currentVal};
if ~isempty(currentStr)
    set(handles.listbox5, 'String',currentStr);
else
    set(handles.listbox5, 'String','');
end
updateMemberCount(handles);
set(handles.listbox5, 'Value',1);
% Hints: contents = cellstr(get(hObject,'String')) returns pulldown_c2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pulldown_c2


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

% Hint: popupmenu 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 selection change in pulldown_c1.
function pulldown_c1_Callback(hObject, eventdata, handles)
% hObject    handle to pulldown_c1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ud = get(handles.pathchooser,'UserData');
currentVal = get(handles.pulldown_c1,'Value');
currentStr = ud.folders{1,currentVal};
if ~isempty(currentStr)
    set(handles.listbox3, 'String',currentStr);
else
    set(handles.listbox3, 'String','');
end
updateMemberCount(handles);
set(handles.listbox3, 'Value',1);
% Hints: contents = cellstr(get(hObject,'String')) returns pulldown_c1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pulldown_c1


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

% Hint: popupmenu 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 edit_classCount_Callback(hObject, eventdata, handles)
% hObject    handle to edit_classCount (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.slider1,'Value',str2num(get(handles.edit_classCount,'String')));
% Hints: get(hObject,'String') returns contents of edit_classCount as text
%        str2double(get(hObject,'String')) returns contents of edit_classCount as a double


% --- Executes during object creation, after setting all properties.
function edit_classCount_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_classCount (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 updateUserData(handles,boxNumber)
ud = get(handles.pathchooser,'UserData');
currentVal1 = get(handles.pulldown_c1,'Value');
currentVal2 = get(handles.pulldown_c2,'Value');
str1 = get(handles.listbox3, 'String');
str2 = get(handles.listbox5, 'String');
if boxNumber == 1
    ud.folders{1,currentVal1} = str1;
    set(handles.listbox5, 'String',ud.folders{1,currentVal2});
    set(handles.listbox5,'Value',min(size(ud.folders{1,currentVal2},1),get(handles.listbox5, 'Value')));
elseif boxNumber == 2
    ud.folders{1,currentVal2} = str2;
    set(handles.listbox3,'Value',min(size(ud.folders{1,currentVal1},1),get(handles.listbox3, 'Value')));
    set(handles.listbox3, 'String',ud.folders{1,currentVal1});
else
    ud.folders{1,currentVal1} = str1;
    ud.folders{1,currentVal2} = str2;
end
set(handles.pathchooser,'UserData',ud);
updateMemberCount(handles);


% updates text under listbox, which shows actual amount of members
function updateMemberCount(handles)
try
ud = get(handles.pathchooser,'UserData');
currentVal1 = get(handles.pulldown_c1,'Value');
currentVal2 = get(handles.pulldown_c2,'Value');
subjects = ud.folders;
set(handles.text_members1,'String',[num2str(length(subjects{currentVal1})) ' members']);
set(handles.text_members2,'String',[num2str(length(subjects{currentVal2})) ' members']);
set(handles.pathchooser,'UserData',ud);
catch
    msgbox({'Selection has been canceled. ';'Will leave data unchanged.'});
end

% --- Executes on button press in pushbutton_rename1.
function pushbutton_rename1_Callback(hObject, eventdata, handles)
ud = get(handles.pathchooser,'UserData');
ud.pattern = cellstr(ud.pattern);
currentVal1 = get(handles.pulldown_c1,'Value');
newName = inputdlg('Rename','Enter new Name',1,{ud.pattern{currentVal1}});
if ~isempty(newName)
    ud.pattern{currentVal1} = newName{:};
    set(handles.pathchooser,'UserData',ud);
    slider1_Callback(hObject, eventdata, handles)
end
% hObject    handle to pushbutton_rename1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton_rename2.
function pushbutton_rename2_Callback(hObject, eventdata, handles)
ud = get(handles.pathchooser,'UserData');
ud.pattern = cellstr(ud.pattern);
currentVal1 = get(handles.pulldown_c2,'Value');
newName = inputdlg('Rename','Enter new Name',1,{ud.pattern{currentVal1}});
if ~isempty(newName)
    ud.pattern{currentVal1} = newName{:};
    set(handles.pathchooser,'UserData',ud);
    slider1_Callback(hObject, eventdata, handles)
end
% hObject    handle to pushbutton_rename2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)