function varargout = fevalCompiled(this, model, EvalFcn, varargin)
% FEVALCOMPILED Evalutes the function EVALFCN with the model MODEL is compiled.
%
% Requires a valid MODEL name and a function handle EVALFCN of the form
% EvalFcn = @LocalEvalCompiled and VARARGIN is the list of arguments.

% Author(s): Bora Eryilmaz
% Revised: 
% Copyright 2000-2004 The MathWorks, Inc.
% $Revision: 1.1.6.1 $ $Date: 2004/11/18 23:44:34 $

% Error flag
flag = false;

% Turn Accelarator mode off (if set) before compiling
newconfig = struct('SimulationMode', 'normal');
oldconfig = this.setupConfigSet(model, newconfig);

% The model associated with this object should be compiled
compiled = strcmp( get_param(model, 'SimulationStatus'), 'paused' );
if ~compiled
  feval( model, [], [], [], 'compile' );
end

try
  % Evaluate function with the model compiled
  [ varargout{1:nargout} ] = EvalFcn(varargin{:});
catch
  flag = true;
end

% Terminate if compiled in this file
if ~compiled
  feval( model, [], [], [], 'term' );
end

% Restore SimulationMode mode
this.setupConfigSet(model, oldconfig);

% Error occurred
if flag
  rethrow(lasterror)
end
