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

% Last Modified by GUIDE v2.5 22-Nov-2010 10:36:47

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @menup_OpeningFcn, ...
                   'gui_OutputFcn',  @menup_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 menup is made visible.
function menup_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 menup (see VARARGIN)

fondo=importdata('.\buttons\fondo.tif');
axes(handles.axesfondo);
image(fondo);
axis off

modelo=varargin{1};
handles.modelo=modelo{1};
handles.posicion=varargin{2};
handles.figureIADV=varargin{3};

delete(handles.figureIADV);

set(handles.figuremenup,'Position',handles.posicion)

if strcmpi(handles.modelo,'C-295')
boton=importdata('.\buttons\botonC295.tif');
end
if strcmpi(handles.modelo,'C-235')
boton=importdata('.\buttons\botonC235.tif');
end
if strcmpi(handles.modelo,'C-235DW')
boton=importdata('.\buttons\botonCN235.tif');
end

% boton3=importdata('.\buttons\botonCN235.tif');
% boton4=importdata('.\buttons\botonA400M.tif');

set(handles.pushbuttonmenu,'CDATA',boton);

handles.ndig=1;
set(handles.popupmenundig,'Value',1);

% Choose default command line output for menup
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes menup wait for user response (see UIRESUME)
uiwait(handles.figuremenup);


% --- Outputs from this function are returned to the command line.
function varargout = menup_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
delete(gcf);

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

IADV(handles.figuremenup);


% --- Executes on button press in pushbuttoncargador.
function pushbuttoncargador_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttoncargador (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
modelo=cell(1);
modelo{1}=handles.modelo;
CSVv1(modelo);


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

[nombrearchivo,ruta,filtro] = uigetfile(['.\',handles.modelo,'\Operaciones\*.txt'], 'Seleccione un archivo de datos');
if nombrearchivo~=0
    intermedio=textscan(nombrearchivo,'%s','Delimiter','.');
    nombrearchivosinext=intermedio{1}(1);
    nombrearchivosinext=nombrearchivosinext{1};
    rutaD=cell(1);    
    rutaD{1}=[ruta,nombrearchivosinext];
    
    fid=fopen([ruta,nombrearchivo]);
    intermedio2=textscan(fid,'%s%s%s%d%s%d%d%d%d%d%d%d%s','Delimiter',',');
    fclose(fid);
    
    modelo=intermedio2{2};
    
    if handles.ndig==1
        NADv03c(rutaD,modelo,0,0);
    elseif handles.ndig==2
        NADv03b(rutaD,modelo,0,0);
    else
        NADv03a(rutaD,modelo,0,0);
    end
    
end

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

[nombrearchivo,ruta,filtro] = uigetfile(['.\',handles.modelo,'\Analisis\*.ana'], 'Seleccione un archivo de analisis');
if nombrearchivo~=0
    fid=fopen([ruta,nombrearchivo]);
    intermedio=textscan(fid,'%s%s%s%d%d%d','Delimiter',',');
    fclose(fid);

    rutaD=intermedio{2}; %no se usará. NAD lee el análisis de nuevo
        
    modelo=intermedio{3};
    
    if handles.ndig==1
        NADv03c(rutaD,modelo,[ruta,nombrearchivo],0);
    elseif handles.ndig==2
        NADv03b(rutaD,modelo,[ruta,nombrearchivo],0);
    else
        NADv03a(rutaD,modelo,[ruta,nombrearchivo],0);
    end
    
end

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

modelo{1}=handles.modelo;
crearperfil(modelo,0);

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

[handles.perfil,ruta,filtro] = uigetfile(['.\',handles.modelo,'\Perfiles\*.per'], 'Seleccione un archivo de perfil');

if handles.perfil~=0
%     try    
            [nombrearchivo,ruta,filtro] = uigetfile(['.\',handles.modelo,'\Operaciones\*.txt'], 'Seleccione un archivo de datos');
            if nombrearchivo~=0
                intermedio=textscan(nombrearchivo,'%s','Delimiter','.');
                nombrearchivosinext=intermedio{1}(1);
                nombrearchivosinext=nombrearchivosinext{1};
                rutaD=cell(1);
                rutaD{1}=[ruta,nombrearchivosinext];
                
                fid=fopen([ruta,nombrearchivo]);
                intermedio2=textscan(fid,'%s%s%s%d%s%d%d%d%d%d%d%d%s','Delimiter',',');
                fclose(fid);
                
                modelo=intermedio2{2};
                
                uiwait(warndlg('Tenga en cuenta que, si el perfil incluye varios eventos, este proceso puede durar varios minutos. Por favor, espere hasta que abra el área de gráficas.', '¡Atención!','non-modal'));
                
                if handles.ndig==1
                    NADv03c(rutaD,modelo,0,handles.perfil);
                elseif handles.ndig==2
                    NADv03b(rutaD,modelo,0,handles.perfil);
                else
                    NADv03a(rutaD,modelo,0,handles.perfil);
                end
                
            end
%     catch error
%         error
%     end
   
end

guidata(hObject,handles);

% --- Executes when user attempts to close figuremenup.
function figuremenup_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figuremenup (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
uiresume(handles.figuremenup);


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

[nombrearchivo,ruta,filtro] = uigetfile(['.\',handles.modelo,'\Operaciones\*.txt'], 'Seleccione un archivo de datos');
if nombrearchivo~=0
    
    fid=fopen([ruta,nombrearchivo]);
    intermedio=textscan(fid,'%s%s%s%s%s%d%d%d%d%d%d%d%s','Delimiter',',');
    fclose(fid);
    
    O=cell(13,1);
    O{1}=intermedio{1};
    O{2}=intermedio{2};
    O{3}=intermedio{3};
    O{4}=intermedio{4};
    O{5}=intermedio{5};
    O{6}=intermedio{6};
    O{7}=intermedio{7};
    O{8}=intermedio{8};
    O{9}=intermedio{9};
    O{10}=intermedio{10};
    O{11}=intermedio{11};
    O{12}=intermedio{12};
    O{13}=intermedio{13};
    
    verop(O);
    
end


% --- Executes on selection change in popupmenundig.
function popupmenundig_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenundig (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 popupmenundig contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenundig

handles.ndig=get(handles.popupmenundig,'Value');

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function popupmenundig_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenundig (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
