function varargout = Fin(varargin)
% FIN M-file for Fin.fig
%      FIN, by itself, creates a new FIN or raises the existing
%      singleton*.
%
%      H = FIN returns the handle to a new FIN or the handle to
%      the existing singleton*.
%
%      FIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FIN.M with the given input arguments.
%
%      FIN('Property','Value',...) creates a new FIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Fin_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Fin_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 Fin
% Last Modified by GUIDE v2.5 20-Nov-2005 13:12:19

%DEFINICIÓN DE LA FUNCIÓN
%Realizamos la contraportada del programa. Pulsando los correspondientes
%botones cerramos todas las ventanas del programa.

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Fin_OpeningFcn, ...
                   'gui_OutputFcn',  @Fin_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 Fin is made visible.
function Fin_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 Fin (see VARARGIN)
% Choose default command line output for Fin
handles.output = hObject;
canal=0;
t=0;
V=0;
video='';
handles.X=varargin{1};
handles.Y=varargin{2};
handles.canales=varargin{3};
handles.tasa_muestreo=varargin{4};
handles.fecha=varargin{5};
handles.hora=varargin{6};
handles.video=video;
handles.t=t;
handles.V=V;
axes(handles.axes1)
A=imread('logo','jpeg');
set(handles.axes1,'NextPlot','new');
legend off
xlabel('');ylabel('');title('')
set(handles.axes1,'YDir','reverse');
set(handles.axes1,'XTick',[]);
set(handles.axes1,'visible','on');
set(handles.axes1,'YTick',[]);
set(handles.axes1,'YtickLabel',[]);
image(A);
handles.canal=canal;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Fin_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;


% --- Executes on button press in finalizar_push.
function finalizar_push_Callback(hObject, eventdata, handles)
% hObject    handle to finalizar_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close all                   %cerramos todas las ventanas abiertas de las figuras
closereq                    %cerramos la interfaz



% --- 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)

% Hints: contents = get(hObject,'String') returns canal_popup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from canal_popup
% canals=get(hObject,'String');
canalv=get(hObject,'Value');
%si es 1 tiene que dar error, si es 2 e la temperatura, 3 la humedad y el
%resto los demás canales
t=0;
V=0;

canal=canalv;
handles.canal=canal;
if handles.canales>=(canal-3) & handles.canal>1
    axes(handles.axes1)
    set(handles.axes1,'NextPlot','replace');
    plot(handles.X,handles.Y(:,canal-1))
    title('Datos Originales')
    xlabel('t (s)')
    if canal==2
        ylabel('Temperatura (ºC)')
        legend('Temperatura')
    elseif canal==3
        ylabel('Humedad (%)')
        legend('Humedad')
    else
        ylabel('Magnitud (V)')
        legend(strcat('Canal',num2str(canal-3)))
    end
    
else
    errordlg('ERROR: El canal seleccionado no está conectado','ERROR')
    handles.canal=1;
    set(handles.canal_popup,'Value',1)
end
guidata(hObject,handles)

% --- 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

% Hint: popupmenu 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 button press in si_push.
function si_push_Callback(hObject, eventdata, handles)
% hObject    handle to si_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% axes(handles.axes1)
if get(handles.manual_push,'BackgroundColor')==[0 0.502 1] | get(handles.digital_push,'BackgroundColor')==[0 0.502 1]
    direc=pwd;
    fichero=strcat(direc,'\pruebas\',handles.video)
    mov=aviread(fichero);
    mplay(mov)
else
    errordlg('ERROR: Debe seleccionar un instante de tiempo','ERROR')
end

% --- Executes on button press in no_push.
function no_push_Callback(hObject, eventdata, handles)
% hObject    handle to no_push (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 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)
handles.t=0;
handles.V=0;
handles.canal=1;
axes(handles.axes1)
A=imread('logo','jpeg');
set(handles.axes1,'NextPlot','new');
legend off
xlabel('');ylabel('');title('')
set(handles.axes1,'YDir','reverse');
set(handles.axes1,'XTick',[]);
set(handles.axes1,'visible','on');
set(handles.axes1,'YTick',[]);
set(handles.axes1,'YtickLabel',[]);
image(A);
set(handles.tiempo_text,'String','');
set(handles.magnitud_text,'String','');
set(handles.video_text,'String','');
set(handles.canal_popup,'Value',1);
set(handles.fec_text,'String','')
set(handles.hor_text,'String','')
set(handles.digital_push,'BackgroundColor',[0.867 0.867 1])
set(handles.manual_push,'BackgroundColor',[0.867 0.867 1])
guidata(hObject,handles)


% --- Executes on button press in manual_push.
function manual_push_Callback(hObject, eventdata, handles)
% hObject    handle to manual_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.canal==1
    errordlg('ERROR: Primero debe seleccionar el canal sobre el que desea trabajar','ERROR')
else
    [tiempo]=introduc_tiempo_video;

    if tiempo==-1
        warndlg('AVISO: No ha seleccionado ningún instante de tiempo','AVISO');
    else
        tmuestreo=handles.tasa_muestreo;
        Y=handles.Y;
        X=handles.X;
        n=length(X);
        canal=handles.canal;
        t=round(tiempo/tmuestreo+1);
        fechab=handles.fecha;
        horab=handles.hora;
        if t>n
            errordlg('ERROR: El intante de tiempo seleccionado no está reflejado en la señal','ERROR');
            set(handles.tiempo_text,'String','');
            set(handles.magnitud_text,'String','')
            set(handles.video_text,'String','')
            set(handles.fec_text,'String','')
            set(handles.hor_text,'String','')
        else
            handles.t=tiempo;
            handles.V=Y(t,canal-1);
            axes(handles.axes1)
            hold on
            plot(tiempo,[min(Y(:,canal-1)):0.001:max(Y(:,canal-1))],'--r')
            hold off
            [fichero,fecha,hora]=captura_de_video(tiempo,fechab,horab);
            handles.video=fichero;
            set(handles.tiempo_text,'String',num2str(tiempo));
            set(handles.magnitud_text,'String',num2str(Y(t,canal-1)));
            set(handles.video_text,'String',fichero);
            set(handles.fec_text,'String',fecha)
            set(handles.hor_text,'String',hora)
            set(handles.manual_push,'BackgroundColor',[0 0.502 1])
            set(handles.digital_push,'BackgroundColor',[0.867 0.867 1])
        end
    end
    guidata(hObject,handles)
end

% --- Executes on button press in digital_push.
function digital_push_Callback(hObject, eventdata, handles)
% hObject    handle to digital_push (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.canal==1
    errordlg('ERROR: Primero debe seleccionar el canal sobre el que desea trabajar','ERROR')
else
    [t,V]=ginput(1);
    tmuestreo=handles.tasa_muestreo;
    canal=handles.canal;
    Y=handles.Y;
    handles.t=t;
    X=handles.X;
    fecha='';
    hora='';
    horab=handles.hora;
    fechab=handles.fecha;
    if t>X(length(X)) | t<0
            errordlg('ERROR: El intante de tiempo seleccionado no está reflejado en la señal','ERROR');
            set(handles.tiempo_text,'String','');
            set(handles.magnitud_text,'String','')
            set(handles.video_text,'String','')
            set(handles.fec_text,'String','')
            set(handles.hor_text,'String','')
    else
        valort=round(t/tmuestreo+1);
        valorv=Y(valort,canal-1);
        handles.V=valorv;
        axes(handles.axes1)
        hold on
        plot(t,[min(Y(:,canal-1)):0.001:max(Y(:,canal-1))],'-r')
        hold off
        [fichero,fecha,hora]=captura_de_video(t,fechab,horab);
        handles.video=fichero;
        set(handles.tiempo_text,'String',num2str(t))
        set(handles.magnitud_text,'String',num2str(valorv))
        set(handles.video_text,'String',fichero)
        set(handles.fec_text,'String',fecha)
        set(handles.hor_text,'String',hora)
        set(handles.digital_push,'BackgroundColor',[0 0.502 1])
        set(handles.manual_push,'BackgroundColor',[0.867 0.867 1])
    end
    guidata(hObject,handles)
end


% --- Executes on button press in zoom_radio.
function zoom_radio_Callback(hObject, eventdata, handles)
% hObject    handle to zoom_radio (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 zoom_radio
 axes(handles.axes1)
if (get(hObject,'Value')==get(hObject,'Max'))  
    zoom on
else
    zoom off
end




% --- 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)
ayuda_video;

