function varargout = seleccionarextremos(varargin)
% SELECCIONAREXTREMOS M-file for seleccionarextremos.fig
%      SELECCIONAREXTREMOS, by itself, creates a new SELECCIONAREXTREMOS or raises the existing
%      singleton*.
%
%      H = SELECCIONAREXTREMOS returns the handle to a new SELECCIONAREXTREMOS or the handle to
%      the existing singleton*.
%
%      SELECCIONAREXTREMOS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SELECCIONAREXTREMOS.M with the given input arguments.
%
%      SELECCIONAREXTREMOS('Property','Value',...) creates a new SELECCIONAREXTREMOS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before seleccionarextremos_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to seleccionarextremos_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
% Copyright 2002-2003 The MathWorks, Inc.
% Edit the above text to modify the response to help seleccionarextremos
% Last Modified by GUIDE v2.5 25-Oct-2005 18:16:09

%DEFINICIÓN DE FUNCIÓN
%Función donde se le solicita al usuario que introduzca el canal sobre el
%cual desea realizar una caracterización de la señal a trozos, y los
%extremos superior e inferior.
%Como parámetros de entrada estan: la tasa de muestreo, el tiempo total de
%los datos y el número de canales conectados
%Como parámetro de salida se muestra el canal, los extremos inferiores y
%superior y una bandera que nos indica la forma de salir de la función
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @seleccionarextremos_OpeningFcn, ...
                   'gui_OutputFcn',  @seleccionarextremos_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 seleccionarextremos is made visible.
function seleccionarextremos_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 seleccionarextremos (see VARARGIN)
% Choose default command line output for seleccionarextremos
%handles.output = hObject;

handles.tmuestreo = varargin{1};
set(handles.max_text,'String',varargin{2});
set(handles.maxy_text,'String',varargin{2});
set(handles.inferior_slider,'Max',varargin{2});
set(handles.superior_slider,'Max',varargin{2});
handles.ttotal  = varargin{2};
handles.canales = varargin{3};

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes seleccionarextremos wait for user response (see UIRESUME)
uiwait(handles.figure1);



% --- Outputs from this function are returned to the command line.
function varargout = seleccionarextremos_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
try
    varargout{1} = handles.canal;
    varargout{2} = handles.superior;
    varargout{3} = handles.inferior;
    varargout{4} = handles.flagaceptar;
catch
    varargout{1} = [];
    varargout{2} = [];
    varargout{3} = [];
    varargout{4} = 0;
end
closereq



% --- Executes on selection change in canal_popup.
function canal_popup_Callback(hObject, eventdata, handles)
% hObject    handle to canal_popup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%Selección del canal sobre el que deseamos realizar la caracterización de
%la señal
% canals = get(hObject,'String');
canalv = get(hObject,'Value');
canal  = canalv;
handles.canal = canal;
guidata(hObject,handles)
%canal 1 es 0, 2 es la temperatura, 3 es la humedad y acontinuación el
%resto de canales

% --- Executes during object creation, after setting all properties.
function canal_popup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to canal_popup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



% --- Executes on slider movement.
function inferior_slider_Callback(hObject, eventdata, handles)
% hObject    handle to inferior_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%Selección del extremo inferior para el canal conectado
valor=0;
valor=get(hObject,'Value');
set(handles.inferior_text,'String',valor);
handles.inferior=(valor);
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function inferior_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to inferior_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



% --- Executes on slider movement.
function superior_slider_Callback(hObject, eventdata, handles)
% hObject    handle to superior_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%Selección del extremo superior para el canal seleccionado
valor = 0;
valor = get(hObject,'Value');
set(handles.superior_text,'String',valor);
handles.superior = (valor);
guidata(hObject,handles)


% --- Executes during object creation, after setting all properties.
function superior_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to superior_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



% --- Executes on button press in aceptar_push.
function aceptar_push_Callback(hObject, eventdata, handles)
% hObject    handle to aceptar_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%Una vez seleccionado el canal y los extremos, se debe pulsar este botón
%para guardar los datos y realizar el procesado de la porción seleccionada
vsuper  = str2double(get(handles.superior_text,'String'));
vsinfer = str2double(get(handles.inferior_text,'String'));

if  get(handles.canal_popup,'Value')==1 
    errordlg('ERROR: Debe seleccionar un canal distinto de 0','ERROR');
    
elseif handles.canales<(handles.canal-3)
    errordlg('ERROR: El canal seleccionado no puede ser mayor que el número de canales conectados','ERROR');
     
elseif (vsuper>vsinfer)  
    handles.superior=vsuper;
    handles.inferior=vsinfer;
    handles.flagaceptar=1;
    guidata(hObject,handles)
    uiresume
   
else
    errordlg('ERROR: El extremo superior debe ser mayor que el inferior','ERROR');
end



% --- Executes on button press in deshacer_push.
function deshacer_push_Callback(hObject, eventdata, handles)
% hObject    handle to deshacer_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%Se reinician todas las variables a los valores por defecto y se solicita
%al usuario que vuelva a introducir todos los datos
set(handles.canal_popup,'Value',1);        
set(handles.superior_slider,'Value',0);
set(handles.inferior_slider,'Value',0);
set(handles.inferior_text,'String','');
set(handles.superior_text,'String','');
handles.superior=0;
handles.inferior=0;
handles.canal=0;
handles.flagaceptar=0;
guidata(hObject,handles)



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



% --- Executes on button press in ayuda_push.
function ayuda_push_Callback(hObject, eventdata, handles)
% hObject    handle to ayuda_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
helpdlg('AYUDA:Seleccione el canal deseado y los extremos superior e inferior','AYUDA');



