www.gusucode.com > sloptim工具箱matlab源码程序 > sloptim/sloptim/@ResponseOptimizer/@SimTest/getCurrentResponse.m

    function RunLog = getCurrentResponse(this,idxRuns)
% Logs current response of Simulink model.

%   Author:
%   Copyright 1986-2012 The MathWorks, Inc.

Model = this.Model;
nspec = length(this.Specs);

% Activate data logging ports for spec's constraints
hOutport = zeros(nspec,1);
tmpEndTime = -inf;
for ct=1:nspec
   C = this.Specs(ct);
   % Find associated port
   hOutport(ct) = find_system(Model,...
      'FindAll','on','FollowLinks','on','LookUnderMasks','all',...
      'Type','port','PortType','outport',...
      'DataLoggingName',C.SignalSource.LogID);
   tmpEndTime = max([tmpEndTime, ...
      C.LowerBoundX(end), ...
      C.UpperBoundX(end)]);
end

% Enable data logging
set(hOutport,'DataLogging','on');


%Create model config set from active model configuration
Tf      = getSimTime(this); %Additional output times
cfg     = this.CreateConfig(Model, tmpEndTime, Tf);
LogName = cfg.getProp('SignalLoggingName');

hWarn = ctrlMsgUtils.SuspendWarnings;
if isempty(this.Runs)
   % Single (nominal) simulation
   try
      simResult = sim(Model,cfg);
      %Update simcount
      this.SimCount = this.SimCount + 1;
      % Save simulation result
      RunLog = {simResult.get(LogName)};
   catch E
      RunLog = {[]};
      if ~strcmp(E.identifier,'Simulink:Commands:SimAborted')
         this.LastSimError = sroengine.ut_stripErrorString(E.message);
      end
   end
else
   % Multi-run test
   % Initialize data log
   RunLog = cell(getGridSize(this.Runs));
   if nargin==1
      idxRuns = 1:numel(RunLog);
   end
   % Save current parameter values
   Params = getParNames(this.Runs);
   NomVals = utEvalParams(Model,Params);
   % Simulate for each trial value of uncertain parameters
   UncVals = NomVals;
   for ct=1:length(idxRuns)
      idxr = idxRuns(ct);
      % Assign values of uncertain parameters
      s = getsample(this.Runs,idxr,Params);
      for ctp=1:length(UncVals)
         UncVals(ctp).Value = s.(UncVals(ctp).Name);
      end
      utAssignParams(Model,UncVals)
      % Simulate
      try %#ok<TRYNC>
         simResult = sim(Model,cfg);
         RunLog{idxr} = simResult.get(LogName);
         %Update simulation counter
         this.SimCount = this.SimCount + 1;
      end
   end
   % Restore original values
   utAssignParams(Model,NomVals)
end
delete(hWarn)

% Deactivate data logging ports
set(hOutport,'DataLogging','off');
end