% *****************************************************************************************
%  Fichier exemple issu du tutoriel : 
%  "Compilation et déploiement d'un modèle Simulink avec Embedded Coder"
%   
%  http://simulink.developpez.com/tutoriels/compilation-deploiement-modele-embedded-coder
%   
%  Auteurs : duf42 et Jérôme Briot pour Developpez.com - 2013   	
% *****************************************************************************************
 
 modelname = 'gestion_reservoir';

arch = computer('arch');

if strcmp(arch,'win64') || strcmp(arch,'win32')
    ext = '.dll';
    libname = [modelname '_' arch];
else
    ext = '.so';
    libname = modelname;
end

if exist('mHeader.m','file')==2
    delete('mHeader.m');
end

if exist(modelname,'dir')==7
    rmdir(modelname,'s');
end
delete([modelname '*.*']);

zipfile = [modelname '.zip'];
libfile = [libname ext];

copyfile(['../lib/' arch '/' libfile],'.');
copyfile(['../lib/' arch '/' zipfile],'.');

unzip(zipfile , modelname);
delete(zipfile)

if exist('test.txt','file')==2
    delete('test.txt');
end

if exist('test','file')~=2
    error('executable not found')
end

if ispc
    system('test > test.txt');
else
    system('./test > test.txt');
end

if exist('test.txt','file')~=2
    error('txt not found')
end

x = load('test.txt');
N = size(x,1);

figure;

subplot(211);
plot(1:N,x(:,1));
title('Niveau courant');

subplot(212);
plot(1:N,x(:,2))
title('Debit courant');