function varargout = uploadVbles(varargin)
% uploadVbles
%   uploadVbles upload variables to the IMPACT data tool

%   Project: IMPACT
%   Title of Code: uploadVbles
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.

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


% --- Executes just before uploadVbles is made visible.
function uploadVbles_OpeningFcn(hObject, eventdata, handles, varargin)
addIMPACTIcon(hObject);

vbles = evalin('base','who');
set(handles.popupmenu_vble,'String',vbles);
set(handles.popupmenu_time,'String',vbles);
handles.ok = 0;
handles.Data = [];

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes uploadVbles wait for user response (see UIRESUME)
uiwait(handles.figure_upload);


% --- Outputs from this function are returned to the command line.
function varargout = uploadVbles_OutputFcn(hObject, eventdata, handles) 
if exist('handles','var')
    if isfield(handles,'Data')
        varargout{1} = handles.Data;
        varargout{2} = handles.ok;
        delete(hObject);
    else
        varargout{1} = [];
        varargout{2} = 0;
    end
else
    varargout{1} = [];
    varargout{2} = 0;
end



% --- Executes on selection change in popupmenu_vble.
function popupmenu_vble_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function popupmenu_vble_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_time.
function popupmenu_time_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function popupmenu_time_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton_ok.
function pushbutton_ok_Callback(hObject, eventdata, handles)
cst_vble = get(handles.popupmenu_vble,'String');
no_vble = get(handles.popupmenu_vble,'Value');
st_vble = cst_vble{no_vble};

cst_time = get(handles.popupmenu_time,'String');
no_time = get(handles.popupmenu_time,'Value');
st_time = cst_time{no_time};
cS = evalin('base',st_vble);
time =evalin('base',st_time);
if isempty(cS)
    errordlgMLS(sprintf('The variable %s is empty',st_vble),'Error');
    return;
end

if numel(size(cS))>2
    errordlgMLS(sprintf('The variable %s has to be a vector',st_vble),'Error');
    return;
end

if ~any(size(cS)==1)
    errordlgMLS(sprintf('The variable %s has to be a vector',st_vble),'Error');
    return;
end

if ~isa(cS,'numeric') || any(isnan(cS))
    errordlgMLS(sprintf('The variable %s has to be numeric',st_vble),'Error');
    return;
end

if isempty(time)
    errordlgMLS(sprintf('The variable %s is empty',st_time),'Error');
    return;
end

if numel(size(time))>2
    errordlgMLS(sprintf('The variable %s has to be a vector',st_vble),'Error');
    return;
end

if ~any(size(time)==1)
    errordlgMLS(sprintf('The variable %s has to be a vector',st_vble),'Error');
    return;
end

if ~isa(time,'numeric') || any(isnan(time))
    errordlgMLS(sprintf('The variable %s has to be numeric',st_time),'Error');
    return;
end

if numel(time)~=numel(cS)
    errordlgMLS('The variables have to have the same number of elements','Error');
    return;
end

if diff(numel(time))<=0
    errordlgMLS(sprintf('The variable %s needs to be increasing',st_time),'Error');
    return;
end
Data.currentSig = cS;
Data.time = time;
Data.name = st_vble;
handles.Data = Data;
handles.ok = 1;
guidata(hObject,handles);
uiresume(handles.figure_upload);


% --- Executes on button press in pushbutton_cancel.
function pushbutton_cancel_Callback(hObject, eventdata, handles)
handles.Data = [];
handles.ok = 0;
guidata(hObject,handles);
uiresume(handles.figure_upload);
