function runMonolix(name, mlxInstallDir, scenario, saveGraphics)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function allowing the run of all the mlxtran projects in a directory
%
% -> name: Define the Mlxtran files to run in absolute path
% --> If it is a list of Mlxtran files, run them
% --> If it is a directory, run all the Mlxtran files in it
% -> mlxInstallDir : Define where MonolixSuite is installed
% -> saveGraphics : Boolean to define if the graphics are saved or not (FALSE by default)
% -> scenario : Monolix scenario could be a list of
%     – saem: estimate population parameters
%     – indiv: estimate individual parameters
%     – fim: estimate the standard errors of the estimates and Fisher information matrix
%     – ll: estimate the log-likelihood
%     – graphics: generate the result graphics
%   By default, the run option is used to get the scenario from the project
% Typically for windows mlxInstallDir <- 'C:/ProgramData/Lixoft/MonolixSuite2016R1/bin/'
%
% Lixoft - v1.0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

runCalculation = true;

% Define the operating system: the associated function is different between Windows and Linux
if(length(strfind(getenv('OS'),'Windows')))
    ext = 'bat';
else
    ext = 'sh';
end
% Check the existence of Monolix
monolixPath = fullfile(mlxInstallDir,['monolix.',ext]);
if(exist(monolixPath, 'file')~=2)
    warningMessage = sprintf('Warning: nable to find monolix.bat with your definition of mlxInstallDir:\n%s', mlxInstallDir);
    uiwait(msgbox(warningMessage));
    runCalculation = false;
end

% Check the existance of all the Mlxtran projects and/or directory
if(~iscell(name))
    name = {name};
end
% Check if it is a list of directory or files
files = {};
index_file = 0;
for(index_name = 1:length(name))
    if(exist(name{index_name},'file')==7) % it is a folder
        filesDir = dir([name{index_name},'*.mlxtran']);
        for index_f =1:length(filesDir)
            index_file = index_file+1;
            files{index_file} = fullfile(name{index_name},filesDir(index_f).name);
        end
    elseif (exist(name{index_name},'file')==2) % it is a file
        index_file = index_file+1;
        files{index_file} = name{index_name};
    else
        warning(['File/folder :',name{index_name},'does not exist \n'])
    end
end

% initialization of the default values
if isempty(saveGraphics)
    saveGraphics = false ;
end
displayCommand='';

% Define the scenario
feasibleTasks ={'saem','SAEM','fim','FIM','ll','LL','graphics','GRAPHICS','indiv','INDIV'};
if(isempty(scenario))
    displayScenario = ' -f RUN';
else
    displayScenario = '';
    if(~iscell(scenario))% Convert in cell in case of single task
        scenario = {scenario};
    end
    for indexScenario = 1:length(scenario)
        task = scenario{indexScenario};
        if(ismember(task,feasibleTasks))
            displayScenario = [displayScenario,' -f ',task];
        else
            warning([task,' in not a valid task for the scenario'])
        end
    end
    
    if(isempty(displayScenario))
        warning('scenario does not contain any valid task')
        runCalculation = false;
    end
end

% Change the graphicsToPrint
if(saveGraphics)
    for index = 1:length(files)
        filesXmlx = strrep(files{index},'.mlxtran','_graphics.xmlx');      
        if(exist(filesXmlx,'file')==2)
            fid = fopen(filesXmlx,'r'); %opening in read mode (default)
            inter = textscan(fid,'%[^\n]'); fclose(fid);
            lines = inter{1};
            isInGraphics = 0;
            for ind_Line = 1:length(lines)
                % Check if we are in the <graphicsToPrint> Section
                if(length(strfind(lines{ind_Line},'<graphicsToPrint>'))>0)
                    isInGraphics = 1;
                elseif (length(strfind(lines{ind_Line},'</graphicsToPrint>'))>0)
                    isInGraphics = 0;
                end
                if(isInGraphics==1)
                    if(length(strfind(lines{ind_Line},'value='))>0)
                        lines{ind_Line}=strrep(lines{ind_Line},'0','1');
                    end
                end
            end
            fid = fopen(fullfile(directory,filesXmlx(index).name),'w');
            for ind_Line = 1:length(lines)
                fprintf(fid,[lines{ind_Line},'\n']);
            end
            fclose(fid);
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute all the projects
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(runCalculation)
    errorDisplay = '\n/***************************************************/ \nSummary \n';
    for index = 1:length(files)
        [pathstrF,nameF,extF] = fileparts(files{index}) ;
        fprintf(['Running project ',nameF,' ']);
        sys_cmd = [monolixPath,' -p ',files{index},displayCommand,displayScenario];
        [status,out] = system(sys_cmd);
        % Check if there were an error
        stringToSearch = 'Error message:';
        indexError = strfind(out,stringToSearch);
        if(length(indexError)>0)
            out_error = out((indexError+length(stringToSearch)):end-1);
            errorDisplay = [errorDisplay,'Project: ',files{index},' NOK \n => ',out_error,'\n'];
            fprintf(['NOK => ',out_error,'\n']);
        else
            fprintf('OK\n')
            errorDisplay = [errorDisplay,'Project: ',files{index},' OK \n'];
        end
    end
    errorDisplay= [errorDisplay,'/***************************************************/\n'];
    fprintf(errorDisplay);
end