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

% Last Modified by GUIDE v2.5 02-Feb-2005 20:42:52

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


% Choose default command line output for programacion_sim
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

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

%Sets the list box directory to the current directory,
%if no directory is specified. Changes the current directory, if a directory is specified. 



if nargin == 3,
    initial_dir = pwd;
elseif nargin == 4 & exist(varargin{1},'dir')
    initial_dir = varargin{1};
else
    errordlg('Input argument must be a valid directory','Input Argument Error!')
    return
end
% Populate the listbox
load_listbox(initial_dir,handles)
set(handles.togglebutton5,'enable','off');
actual=clock;
set(handles.edit1,'string',actual(1,3));
set(handles.edit2,'string',actual(1,4));
set(handles.edit14,'string',actual(1,5));
set(handles.edit3,'string',actual(1,4));
set(handles.edit15,'string',actual(1,5));
set(handles.text27,'string',0);
for i=5:10
    set(eval(strcat('handles.edit',num2str(i))),'string','0');
end





% --- Outputs from this function are returned to the command line.
function varargout = programacion_sim_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 during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


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



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


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



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


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


function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double

texto1=strcat(get(handles.edit12,'string'),'/','Referencia variable');
if get(handles.edit12,'string')==''''
     errordlg(lasterr,'Campo Archivos vacio','modal');
     return;
 end
open(get(handles.edit12,'string'));
set_param(texto1,'Tref1',get(handles.edit5,'string'));
save_system(get(handles.edit12,'string'));
bdclose(get(handles.edit12,'string'));

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



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double
texto1=strcat(get(handles.edit12,'string'),'/','Referencia variable');
if get(handles.edit12,'string')==''''
     errordlg(lasterr,'Campo Archivos vacio','modal');
     return;
 end
open(get(handles.edit12,'string'));
set_param(texto1,'Tref2',get(handles.edit6,'string'));
save_system(get(handles.edit12,'string'));
bdclose(get(handles.edit12,'string'));


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



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double

texto1=strcat(get(handles.edit12,'string'),'/','Referencia variable');
if get(handles.edit12,'string')==''''
     errordlg(lasterr,'Campo Archivos vacio','modal');
     return;
 end
open(get(handles.edit12,'string'));
set_param(texto1,'Tref3',get(handles.edit7,'string'));
save_system(get(handles.edit12,'string'));
bdclose(get(handles.edit12,'string'));

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



function edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double

texto1=strcat(get(handles.edit12,'string'),'/','Referencia variable');
if get(handles.edit12,'string')==''''
     errordlg(lasterr,'Campo Archivos vacio','modal');
     return;
 end
open(get(handles.edit12,'string'));
set_param(texto1,'Tref4',get(handles.edit8,'string'));
save_system(get(handles.edit12,'string'));
bdclose(get(handles.edit12,'string'));

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



function edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double

texto1=strcat(get(handles.edit12,'string'),'/','Referencia variable');
if get(handles.edit12,'string')==''''
     errordlg(lasterr,'Campo Archivos vacio','modal');
     return;
 end
open(get(handles.edit12,'string'));
set_param(texto1,'Tref5',get(handles.edit9,'string'));
save_system(get(handles.edit12,'string'));
bdclose(get(handles.edit12,'string'));

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



function edit10_Callback(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a double

texto1=strcat(get(handles.edit12,'string'),'/','Referencia variable');
if get(handles.edit12,'string')==''''
     errordlg(lasterr,'Campo Archivos vacio','modal');
     return;
 end
open(get(handles.edit12,'string'));
set_param(texto1,'Tref6',get(handles.edit10,'string'));
save_system(get(handles.edit12,'string'));
bdclose(get(handles.edit12,'string'));

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


%Identificacion errores posibles.
if get(handles.edit12,'string')==''''
    errordlg(lasterr,'Campo de archivos vacio','modal');
    return;
end
if get(handles.edit2,'string')==' ' | get(handles.edit3,'string')==' '
    errordlg(lasterr,'Campo de horas llenado incorrectamente','modal');
    return;
end
if get(handles.edit1,'string')==' '
    errordlg(lasterr,'Campo de dias incorrectamente llenado','modal');
    return;
end

tiempo=-(str2num(get(handles.edit2,'string'))+str2num(get(handles.edit14,'string'))/60)+(str2num(get(handles.edit3,'string'))+str2num(get(handles.edit15,'string'))/60);
    

if tiempo<=1
        if get(handles.edit5,'string')=='0'
            errordlg(lasterr,'Campo set point  incorrecto','modal');
            set(handles.edit10,'enable','on');
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
    set(handles.edit7,'enable','on');
    set(handles.edit8,'enable','on');
    set(handles.edit9,'enable','on');
            return;
        end
    end
    if tiempo>1 & tiempo<=2
        if get(handles.edit5,'string')=='0' | get(handles.edit6,'string')=='0'
            errordlg(lasterr,'Campo set point  incorrecto','modal');
        set(handles.edit10,'enable','on');
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
    set(handles.edit7,'enable','on');
    set(handles.edit8,'enable','on');
    set(handles.edit9,'enable','on');
            return;    
        end
    end
    
    if tiempo>2 & tiempo<=3
        if get(handles.edit5,'string')=='0' | get(handles.edit6,'string')=='0' | get(handles.edit7,'string')=='0';
            errordlg(lasterr,'Campo set point  incorrecto','modal');
            set(handles.edit10,'enable','on');
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
    set(handles.edit7,'enable','on');
    set(handles.edit8,'enable','on');
    set(handles.edit9,'enable','on');
            return;
        end
    end
    if tiempo>3 & tiempo<=4
        if get(handles.edit5,'string')=='0' | get(handles.edit6,'string')=='0' | get(handles.edit7,'string')=='0' | get(handles.edit8,'string')=='0';
            errordlg(lasterr,'Campo set point  incorrecto','modal');
            set(handles.edit10,'enable','on');
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
    set(handles.edit7,'enable','on');
    set(handles.edit8,'enable','on');
    set(handles.edit9,'enable','on');
            return;
        end
    end
    
    if tiempo>4 & tiempo<=5;
        if get(handles.edit5,'string')=='0' | get(handles.edit6,'string')=='0' | get(handles.edit7,'string')=='0'| get(handles.edit8,'string')=='0' | get(handles.edit9,'string')=='0';
            errordlg(lasterr,'Campo set point  incorrecto','modal');
            set(handles.edit10,'enable','on');
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
    set(handles.edit7,'enable','on');
    set(handles.edit8,'enable','on');
    set(handles.edit9,'enable','on');
            return;
        end
    end
    if tiempo>5 & tiempo<=6
        if get(handles.edit5,'string')=='0' | get(handles.edit6,'string')=='0' | get(handles.edit7,'string')=='0'| get(handles.edit8,'string')=='0'| get(handles.edit9,'string')=='0'| get(handles.edit10,'string')=='0';
            errordlg(lasterr,'Campo set point  incorrecto','modal');
            set(handles.edit10,'enable','on');
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
    set(handles.edit7,'enable','on');
    set(handles.edit8,'enable','on');
    set(handles.edit9,'enable','on');
            return;
        end
    end
    %fin identificacion errores posibles
    set(handles.edit10,'enable','inactive');
    set(handles.edit5,'enable','inactive');
    set(handles.edit6,'enable','inactive');
    set(handles.edit7,'enable','inactive');
    set(handles.edit8,'enable','inactive');
    set(handles.edit9,'enable','inactive');
    tiempo1=clock;
    tiempo2=clock;
    tiempo2=[tiempo2(1,1) tiempo2(1,2) str2num(get(handles.edit1,'string')) str2num(get(handles.edit2,'string')) str2num(get(handles.edit14,'string')) 0];
    inc_tiempo=etime(tiempo2,tiempo1);
     vis_label=inc_tiempo/10;
  set(handles.togglebutton5,'enable','on');
  % Lectura estado bomba via OPC
  lectura_estado_bomba      
  
      %  h=get(handles.edit16,'string');
       % mxOPC('Verbose',4);
        %mxOPC('ReadMode','cache');
        %hr=mxOPC('open','OPC.Cube.1',h,500);
        %if (hr~=0)
        %hr=mxOPC('open','OPC.Cube.1',h,500);
	        %if (hr~=0)
	        %errordlg(lasterr,'Campo clientes llenado incorrectamente','modal');
         %   return;
         %  end
         %end
        %Lectura estado bomba
         %hr=mxopc('setboolcache','SOLAR/mmd10B01_cmd',1,0);
         %if hr==0
   	    %sprintf('Cache OK para item %s',ite2)
        %else
       % hr=mxOPC('setboolcache',ite,1,0);
			%if hr==0
   				%sprintf('Cache OK para item %s',ite2)
                %else
         	%sprintf('ERROR STABLISHING CACHE FOR item bomba')
         %   mxopc('close');
          %  disp('FAILED COMUNNICATION WITH OPC SERVER');
           % return;
           %end
           %end
        %h22=mxopc('readcache');
        %if h22==0
   	     
         %else
         %   h22=mxopc('readcache');
      	   %     if h22==0
   			    %disp('LECTURA COMPLETA DE CACHE OK')
                %else
				%disp('ERROR READING CACHE')
     %           mxopc('close');
      %          disp('FAILED COMMUNICATION WITH OPC SERVER');
       %          return;
       %        end
       % end
       % [PV hr]=mxopc('readbool','SOLAR/mmd10B01_cmd');;
  	    %if hr==0
   	    %sprintf('LECTURA OK para item %s',ite2)
        % else
        %        [PV,hr]=mxOPC('readbool','SOLAR/mmd10B01_cmd');
			   % if hr==0
   				%sprintf('LECTURA OK para item %s',ite2)
                %else
         	    %sprintf('ERROR READING item bomba')
              %  mxopc('close');
               % disp('FAILED COMMUNICATION WITH OPC SERVER');
                %return;
                %end
                %end
        %desconexion servidor
       % mxopc('close')
       PV=lectura_estado_bomba;
     
        if PV==0
         set(handles.togglebutton6,'value',0);
          set(handles.togglebutton6,'string','OFF');
    set(handles.togglebutton6,'backgroundcolor',[1,0,0]);
    escritura_bomba(0);
    disp('BOMBA OFF');
else
         set(handles.togglebutton6,'value',1);
          set(handles.togglebutton6,'string','ON');
    set(handles.togglebutton6,'backgroundcolor',[0,1,0]);
    escritura_bomba(1);
    
end
     
     
     j=1;
     k=18;
      
     for i=1:inc_tiempo
         restante=inc_tiempo-i;
         contador=restante;
         restante=int2str(restante);
         set(handles.text27,'string',restante);
         if get(handles.togglebutton5,'value')==1;
             set(handles.togglebutton5,'value',0);
             set(handles.text27,'string','0');
             set(handles.togglebutton5,'enable','off');
             return;
         end
         pause(1);
          if i>=j*vis_label
             texto=strcat('handles.text',num2str(k));
             set(eval(texto),'visible','on');
             k=k+1;
             j=j+1;
         end
         if contador <= 3600 & get(handles.togglebutton6,'value')==0
              set(handles.togglebutton6,'value',1);
             set(handles.togglebutton6,'string','ON');
   set(handles.togglebutton6,'backgroundcolor',[0,1,0]);
   escritura_bomba(1);
         end
 end
 set(handles.text27,'string','0');
     
     
     
     
     
     for i=18:26
         texto=strcat('handles.text',num2str(i));
         set(eval(texto),'visible','off');
     end
     tiempo3=[tiempo2(1,1) tiempo2(1,2) str2num(get(handles.edit1,'string')) str2num(get(handles.edit3,'string')) str2num(get(handles.edit15,'string')) 0];
     t=etime(tiempo3,tiempo2);
     t=num2str(t);
     nombre_archivo=get(handles.edit12,'string');
     save nombre_archivo.mat nombre_archivo;
     Run('mimico');
     set_param(get(handles.edit12,'string'),'StartTime','0','StopTime',t);
     set_param(get(handles.edit12,'string'),'SimulationCommand','Start');
     set(handles.edit10,'enable','on');
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
    set(handles.edit7,'enable','on');
    set(handles.edit8,'enable','on');
    set(handles.edit9,'enable','on');
    
 

        


% --- Executes on button press in pushbutton22.
function pushbutton22_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton22 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close('programacion_sim');
run('solarsim');

% --- Executes during object creation, after setting all properties.
function listbox1_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.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
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 = get(hObject,'String') returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1

if strcmp(get(handles.figure1,'SelectionType'),'open') % If double click
    index_selected = get(handles.listbox1,'Value');
    file_list = get(handles.listbox1,'String');
    filename = file_list{index_selected}; % Item selected in list box
    if  handles.is_dir(handles.sorted_index(index_selected)) % If directory
        cd (filename)
        load_listbox(pwd,handles) % Load list box with new directory
    else
        [path,name,ext,ver] = fileparts(filename);
        switch ext
            case '.mdl' 
                set(handles.edit12,'string',name) % Open FIG-file with guide command
            case '.MDL'
                set(handles.edit12,'string',name) % Open FIG-file with guide command

        otherwise 
            errordlg(lasterr,'File Type Error','modal')
    end
    
    end
    for i=5:10
    set(eval(strcat('handles.edit',num2str(i))),'string','0');
end
end


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



function edit12_Callback(hObject, eventdata, handles)
% hObject    handle to edit12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit12 as text
%        str2double(get(hObject,'String')) returns contents of edit12 as a double


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



function edit14_Callback(hObject, eventdata, handles)
% hObject    handle to edit14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit14 as text
%        str2double(get(hObject,'String')) returns contents of edit14 as a double


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



function edit15_Callback(hObject, eventdata, handles)
% hObject    handle to edit15 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit15 as text
%        str2double(get(hObject,'String')) returns contents of edit15 as a double



function load_listbox(dir_path, handles)
%This example creates a subfunction to load items into the list box. 
%This subfunction accepts the path to a directory and the handles 
%structure as input arguments. 
%It performs these steps:
%Change to the specified directory so the GUI can navigate up and down 
%the tree as required. 
%Use the dir command to get a list of files in the specified directory 
%and to determine which name is a directory and which is a file. dir 
%returns a structure (dir_struct) with two fields, 
%name and isdir, which contain this information. 
%Sort the file and directory names (sortrows) and save the sorted names 
%and other information in the handles structure so this information
%can be passed to other functions. 
%The name structure field is passed to sortrows as a cell array, 
%which is transposed to get one file name per row. 
%The isdir field and the sorted index values, 
%sorted_index, are saved as vectors in the handles structure. 
%Call guidata to save the handles structure. 
%Set the list box String property to display the file and directory 
%names and set the Value property to 1. 
%This is necessary to ensure Value never exceeds the number of items in String,
%since MATLAB updates the Value property only when a selection occurs and not when 
%the contents of String changes. 
%Displays the current directory in the text box by setting its String property 
%to the output of the pwd command. 
%The load_listbox function is called by the opening function of the GUI M-file 
%as well as by the list box callback.


cd (dir_path)
dir_struct = dir(dir_path);
[sorted_names,sorted_index] = sortrows({dir_struct.name}');
handles.file_names = sorted_names;
handles.is_dir = [dir_struct.isdir];
handles.sorted_index = [sorted_index];
guidata(handles.figure1,handles)
set(handles.listbox1,'String',handles.file_names,...
    'Value',1)
set(handles.text1,'String',pwd)


% --- Executes on button press in togglebutton5.
function togglebutton5_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.togglebutton5,'value')==1;
for i=18:26
         texto=strcat('handles.text',num2str(i));
         set(eval(texto),'visible','off');
     end
set(handles.edit10,'enable','on');
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
    set(handles.edit7,'enable','on');
    set(handles.edit8,'enable','on');
    set(handles.edit9,'enable','on');
 end
 
% Hint: get(hObject,'Value') returns toggle state of togglebutton5


% --- Executes on button press in togglebutton6.
function togglebutton6_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.togglebutton6,'value')==0
    set(handles.togglebutton6,'string','OFF');
    set(handles.togglebutton6,'backgroundcolor',[1,0,0]);
    escritura_bomba(0);
    % Escritura estado bomba via OPC
        %Abriendo conexion via OPC
       % h=get(handles.edit16,'string');
        %mxOPC('Verbose',4);
        %mxOPC('ReadMode','cache');
        %hr=mxOPC('open','OPC.Cube.1',h,500);
        %if (hr~=0)
        %hr=mxOPC('open','OPC.Cube.1',h,500);
	        %if (hr~=0)
	        %errordlg(lasterr,'Campo clientes llenado incorrectamente','modal');
          %  return;
          % end
          %end
        %Escritura estado bomba
          % hr=mxOPC('setboolcache','SOLAR/mmd10B01_cmd',0,1); 
           %if hr==0
           	%sprintf('Cache OK para item %s',ite2)
            %else
            %    hr=mxOPC('setdoublecache','SOLAR/mmd10B01_cmd',0,1);
            	%if hr==0
           			%	sprintf('Cache OK para item %s',ite2)
                    %      else
               	%sprintf('ERROR ESTABLISHING CACHE FOR item bomba')
                 %   mxopc('close');
                %disp('FAILED COMUNNICATION WITH OPC SERVER');
                %return
                %end
                %end
        
      %h22=mxOPC('writebool','SOLAR/mmd10B01_cmd',0);
   		%if h22==0
   			%sprintf('ESCRITURA OK para item %s',ite2)
            %else
   			%h22=mxOPC('writebool','SOLAR/mmd10B01_cmd',0);
			%if h22==0
  				%sprintf('ESCRITURA OK para item %s',ite2)
                %       	else
                %sprintf('ERROR WRITING item %s',ite2)
            	%mxopc('close');
              %  disp('FAILED COMMUNICATION WITH OPC SERVER');
               % return;
               %end
               %end
        %mxopc('close')
        %Fin escritura Off bomba
      
    end

if get(handles.togglebutton6,'value')==1
    set(handles.togglebutton6,'string','ON');
    set(handles.togglebutton6,'backgroundcolor',[0,1,0]);
    escritura_bomba(1);
    % Escritura estado bomba via OPC
        %Abriendo conexion via OPC
        %h=get(handles.edit16,'string');
        %mxOPC('Verbose',4);
        %mxOPC('ReadMode','cache');
        %hr=mxOPC('open','OPC.Cube.1',h,500);
        %if (hr~=0)
        %hr=mxOPC('open','OPC.Cube.1',h,500);
	        %if (hr~=0)
	        %errordlg(lasterr,'Campo clientes llenado incorrectamente','modal');
         %   return;
         %  end
         % end
        %Escritura estado bomba
         %  hr=mxOPC('setboolcache','SOLAR/mmd10B01_cmd',0,1); 
          % if hr==0
           	%sprintf('Cache OK para item %s',ite2)
            %else
            %    hr=mxOPC('setdoublecache','SOLAR/mmd10B01_cmd',0,1);
            	%if hr==0
           			%	sprintf('Cache OK para item %s',ite2)
                    %      else
               	%sprintf('ERROR ESTABLISHING CACHE FOR item bomba')
                 %   mxopc('close');
                %disp('FAILED COMUNNICATION WITH OPC SERVER');
                %return
                % end
                %end
        
      %h22=mxOPC('writebool','SOLAR/mmd10B01_cmd',1);
   		%if h22==0
   			%sprintf('ESCRITURA OK para item %s',ite2)
            %else
   			%h22=mxOPC('writebool','SOLAR/mmd10B01_cmd',1);
			%if h22==0
  				%sprintf('ESCRITURA OK para item %s',ite2)
                %       	else
                %sprintf('ERROR WRITING item %s',ite2)
            	%mxopc('close');
              %  disp('FAILED COMMUNICATION WITH OPC SERVER');
               % return;
               %end
               %end
        %mxopc('close')
        %Fin escritura On bomba
   %disp('bomba on');
end
% Hint: get(hObject,'Value') returns toggle state of togglebutton6


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



function edit16_Callback(hObject, eventdata, handles)
% hObject    handle to edit16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit16 as text
%        str2double(get(hObject,'String')) returns contents of edit16 as a double


function lectura_bomba=lectura_estado_bomba
disp('leyendo bomba');
PV=0;
lectura_bomba=PV;
function escritura_bomba(var)
if var==0
    disp('Bomba off');
end
if var==1
    disp('bomba ON');
end

    
    