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

% Last Modified by GUIDE v2.5 16-Nov-2004 19:28:10

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


%---Return if the figure is already open
%--- (only allow 1 instance of the demo)
persistent felix;
% End initialization code - DO NOT EDIT
% Create a persistent variable to hold data required by the demo
load nombre_archivo.mat;
archv=nombre_archivo;
archv1=strcat(nombre_archivo,'/salidareal');
felix = struct(...
    'RunModeStatus','Stop', ...
    'Simulink',struct( ...
    'Model',archv, ...
    'GetData',archv1));

% Create a data structure to hold update function and figure handles
persistent LinkData;
LinkData = struct( ...
    'UpDateFcn',@localUpDateDisplays, ...
    'StartFcn',@pushbutton1_Callback, ...
    'StopFcn',@pushbutton2_Callback);
%====================================
% Simulink file must be open for variables to be passed to it


%open_system(felix.Simulink.Model);
%felix.Simulink.WindowOpen = 1;
% Set the user data field of the heatex_getdata S function block
%set_param(felix.Simulink.GetData,'UserData',LinkData);

% set the Simulink callback functions
%set_param(felix.Simulink.Model,'StartFcn','mimicosim_clbk(''Start'')');
%set_param(felix.Simulink.Model,'StopFcn','mimicosim_clbk(''Stop'')');
% --- Executes just before mimicosim is made visible.


function mimicosim_OpeningFcn(hObject, felix, 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 mimicosim (see VARARGIN)

% Choose default command line output for mimicosim
persistent a;

load nombre_archivo.mat;
archv=nombre_archivo;
archv1=strcat(nombre_archivo,'/salidareal');


felix = struct(...
    'RunModeStatus','Stop', ...
    'Simulink',struct( ...
    'Model',archv, ...
    'GetData',archv1));
LinkData = struct( ...
    'UpDateFcn',@localUpDateDisplays, ...
    'StartFcn',@pushbutton1_Callback, ...
    'StopFcn',@pushbutton2_Callback);
handles.output = hObject;
a=handle2struct(handles.output);
save a.mat;
% Update handles structure
guidata(hObject, handles);
open_system(felix.Simulink.Model);
felix.Simulink.WindowOpen = 1;
% Set the user data field of the heatex_getdata S function block
set_param(felix.Simulink.GetData,'UserData',LinkData);
% UIWAIT makes mimicosim wait for user response (see UIRESUME)
% uiwait(handles.figure1);




% --- Outputs from this function are returned to the command line.
function varargout = mimicosim_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 edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 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


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


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, felix, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
load nombre_archivo.mat;
archv=nombre_archivo;
archv1=strcat(nombre_archivo,'/salidareal');

felix = struct(...
    'RunModeStatus','Stop', ...
    'Simulink',struct( ...
    'Model',archv, ...
    'GetData',archv1));
felix.RunModeStatus = 'Start';
set_param(felix.Simulink.Model,'SimulationCommand','Start');


%if  isempty(find_system('Name','pidensayo1Irr')),
%open_system('C:\Documents and Settings\Propietario\Mis documentos\Proyecto\Experimentos\mimicosim\pidensayo1Irr_d_prueba')
%[t,x,y]=sim('C:\Documents and Settings\Propietario\Mis documentos\Proyecto\Experimentos\mimicosim\pidensayo1Irr_d_prueba');
%for i=1:t
%set(handles.edit1,'string',x(i,15));
%end        
%end

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

archv=nombre_archivo;
archv1=strcat(nombre_archivo,'/salidareal');

felix = struct(...
   'RunModeStatus','Stop', ...
  'Simulink',struct( ...
 'Model',archv, ...
'GetData',archv1));
felix.RunModeStatus = 'Stop';
set_param(felix.Simulink.Model,'SimulationCommand','Stop');
pb1=get(handles.pushbutton1,'visible');
pb3=get(handles.pushbutton3,'visible');
if 'pb1==off'
set(handles.pushbutton1,'visible','on');
end
if 'pb3==off'
set(handles.pushbutton3,'visible','on');
end
%%%%%%%%%%%%%%%%%%%%%%%
% localUpDateDisplays %
%%%%%%%%%%%%%%%%%%%%%%%
function localUpDateDisplays(t,u,a)
% This function is called from Simulink when flag = 2 in heatex_getdata.m
% It is passed the GUI data and the Simulation time t, and outputs u
persistent time
persistent valvula
persistent temperatura
persistent set_point
if t==0;
    time=0;
temperatura=0;
set_point=0;
valvula=0;
end
entry=u;
load a;
figure(handles.figure1);
if t==0
set(handles.pushbutton1,'visible','off')
set(handles.pushbutton3,'visible','off')
end
set(handles.edit5,'String',entry(1,1));
set(handles.edit3,'String',entry(2,1));
set(handles.edit2,'String',entry(3,1));
set(handles.edit4,'String',entry(4,1));
set(handles.edit1,'String',entry(5,1));
set(handles.edit6,'String',entry(6,1));
i=t+1;
time(i)=t;
valvula(i)=entry(5,1);
temperatura(i)=entry(4,1);
set_point(i)=entry(2,1);
if t>0;
    for y=2:t
        if time(y)==0
            time(y)=time(y-1);
        end
        if valvula(y)==0;
            valvula(y)=valvula(y-1);
        end
         if temperatura(y)==0
            temperatura(y)=temperatura(y-1);
        end
         if set_point(y)==0
            set_point(y)=set_point(y-1);
        end
    end
end
plot(time,temperatura,'g',time,set_point,'parent',handles.axes3);
plot(time,valvula,'r','parent',handles.axes4);


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%felix = struct(...
 %   'RunModeStatus','Stop', ...
  %  'Simulink',struct( ...
   % 'Model','pidensayo1irr_d_prueba', ...
    %'GetData','pidensayo1irr_d_prueba/salidareal'));
user_response = modaldlg1('Title','GUARDAR');
switch user_response
case 'No'
run('solarsim');
bdclose('all');
close('mimicosim');
case 'Si'
run('Guardar_ensayosim');
end
%run('pantallaprincipal');
%bdclose('all');
%close('mimicosim');

