跳到主要内容
知仓学习社ZHICANG

simulink-baseline-test

Create a MATLAB baseline regression test class for a Simulink model. Use when asked to create a baseline test, golden-reference test, or regression …

不碰外部(只输出文字)无严重或高危命中hashgraph-online/awesome-codex-plugins

它会碰到什么

扫了多少1 个文本文件,6 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

Simulink Baseline Test Creation

Creates a MATLAB test class that captures and validates Simulink model outputs against a saved baseline.

Workflow

Step 1: Analyze the Model

Resolve the model name and explore its structure to select 2–5 signals representative of the model's key behavior.

modelName = bdroot(gcs);

% List top-level subsystems
opts = Simulink.FindOptions;
opts.SearchDepth = 1;
topBlocks = getfullname(Simulink.findBlocks(modelName, opts));
for i = 1:numel(topBlocks)
    bt = get_param(topBlocks{i}, 'BlockType');
    fprintf('  [%s] %s\n', bt, topBlocks{i});
end

Explore subsystem ports, bus selectors, scopes, and outports to understand signal flow. Look for signals that best represent the model's overall behavior — plant outputs, actuator commands, sensor readings, or any other meaningful quantities.

Step 2: Configure Signal Logging

Enable logging on selected output ports:

ph = get_param('<block path>', 'PortHandles');
set(ph.Outport(1), 'DataLogging', 'on');
set(ph.Outport(1), 'Name', '<signalName>');

Never use To Workspace blocks for signal capture.

Step 3: Capture Baseline

Simulate and save the full logsout Dataset — not individual timeseries:

in = Simulink.SimulationInput(modelName);
out = sim(in);

baselineLogsout = out.logsout;
save(fullfile(modelDir, 'baselineData.mat'), 'baselineLogsout');

Step 4: Write the Test Class

Follow these mandatory conventions:

Inherit from sltest.TestCase

classdef MyModelBaselineTest < sltest.TestCase

Not matlab.unittest.TestCase. This gives access to verifySignalsMatch.

Single simulation, single comparison

Simulate the model once. Compare all signals in one call using verifySignalsMatch:

testCase.verifySignalsMatch(out.logsout, S.baselineLogsout, ...
    'RelTol', testCase.RelTol, ...
    'AbsTol', testCase.AbsTol);

Never loop through individual signals. Never simulate once per signal.

Use RelTol and AbsTol directly

Pass 'RelTol' and 'AbsTol' as name-value arguments. MATLAB applies RelTol element-wise per sample; AbsTol acts as a floor for values near zero. Never manually scale tolerances (e.g., max(abs(data)) * relTol).

Smart model teardown with bdIsLoaded

Only close the model if it was not already loaded before the test:

wasLoaded = bdIsLoaded(testCase.ModelName);
load_system(testCase.ModelName);
if ~wasLoaded
    testCase.addTeardown(@()close_system(testCase.ModelName, 0));
end

Do NOT manually run PreLoadFcn

load_system automatically triggers the model's PreLoadFcn callback. Never call evalin('base', get_param(model, 'PreLoadFcn')).

Include a static generateBaseline() method

For easy re-baselining after intentional changes:

methods (Static)
    function generateBaseline()
        modelName = MyModelBaselineTest.ModelName;
        load_system(modelName);
        in = Simulink.SimulationInput(modelName);
        out = sim(in);
        baselineLogsout = out.logsout; %#ok<NASGU>
        save(MyModelBaselineTest.BaselineFile, 'baselineLogsout');
        close_system(modelName, 0);
    end
end

Step 5: Run and Verify

Execute the test and confirm all signals match:

results = runtests('MyModelBaselineTest');

Complete Test Class Template

classdef MyModelBaselineTest < sltest.TestCase

    properties (Constant)
        ModelName    = 'MyModel'
        BaselineFile = fullfile(fileparts(mfilename('fullpath')), 'baselineData.mat')
        RelTol = 1e-6
        AbsTol = 1e-8
    end

    methods (TestClassSetup)
        function loadModelAndBaseline(testCase)
            wasLoaded = bdIsLoaded(testCase.ModelName);
            load_system(testCase.ModelName);
            if ~wasLoaded
                testCase.addTeardown(@()close_system(testCase.ModelName, 0));
            end

            testCase.assertTrue(isfile(testCase.BaselineFile), ...
                sprintf('Baseline not found: %s\nRun %s.generateBaseline() first.', ...
                testCase.BaselineFile, mfilename('class')));
        end
    end

    methods (Test)
        function testAllSignalsMatchBaseline(testCase)
            in  = Simulink.SimulationInput(testCase.ModelName);
            out = sim(in);

            S = load(testCase.BaselineFile, 'baselineLogsout');

            testCase.verifySignalsMatch(out.logsout, S.baselineLogsout, ...
                'RelTol', testCase.RelTol, ...
                'AbsTol', testCase.AbsTol);
        end
    end

    methods (Static)
        function generateBaseline()
            modelName = MyModelBaselineTest.ModelName;
            load_system(modelName);

            in  = Simulink.SimulationInput(modelName);
            out = sim(in);

            baselineLogsout = out.logsout; %#ok<NASGU>
            save(MyModelBaselineTest.BaselineFile, 'baselineLogsout');
            fprintf('Baseline saved to: %s\n', MyModelBaselineTest.BaselineFile);

            close_system(modelName, 0);
        end
    end
end

Anti-Patterns — Do NOT

  • Simulate once per signal (parameterized tests calling sim each time)
  • Loop through signals to verify individually — use verifySignalsMatch
  • Manually scale tolerances (max(abs(data)) * relTol) — use 'RelTol' directly
  • Save individual timeseries — save the full logsout Dataset
  • Unconditionally close the model — check bdIsLoaded first
  • Manually call PreLoadFcnload_system handles it
  • Use matlab.unittest.TestCase — use sltest.TestCase for verifySignalsMatch

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。

它属于哪个仓库

星标★ 1,027
本站分层T1
该仓技能数1910
原文件路径plugins/summer521521/MATLAB_Simulink_plugin/skills/simulink-baseline-test/SKILL.md

同一个仓库里的其他技能

看这个仓库的全部 1910 个技能