Main Content

load

Class: matlabtest.baselines.MATFileBaseline
Namespace: matlabtest.baselines

Load baseline data into workspace

Since R2024b

Description

data = load(baseline) loads the baseline data from the MAT file specified by baseline into the MATLAB® workspace. The method returns the baseline data as a value of any data type.

You can inspect the loaded data to debug test failures. For more information about baseline tests, see Create Baseline Tests for MATLAB Code.

example

Input Arguments

expand all

Representation of the baseline data, specified as a matlabtest.baselines.MATFileBaseline object.

Examples

expand all

Use the load method to load baseline data when running a baseline test in debug mode. For more information on debugging MATLAB files, see Debug MATLAB Code Files.

In your current folder, create a MAT file named testdata.mat that contains the expected output of the magic function when called with 5 as its input.

M = magic(5);
save("testdata.mat","M")

In a file named ExampleTest.m in your current folder, create the ExampleTest test class with a baseline test that compares a value against the baseline data in testdata.mat. If you run this test class, it fails due to an intentional error.

classdef ExampleTest < matlab.unittest.TestCase
    properties (TestParameter)        
        baseline = matlabtest.parameters.matfileBaseline("testdata.mat")
    end

    methods (Test)
        function baselineTest(testCase,baseline)
            actual = magic(6);  % Intentional error
            testCase.verifyEqualsBaseline(actual,baseline)
        end
    end
end

In the Editor, add a breakpoint at the line that includes the verification. Then, run the test, for instance, by executing runtests("ExampleTest") in the Command Window. MATLAB enters debug mode and enables you to inspect the variables in the test.

After running the ExampleTest class from the Command Window, MATLAB in debug mode stops on a breakpoint in the baselineTest method code.

Compare the actual value against the baseline data. To access the baseline data from the baseline variable in the test, use the load method. The comparison indicates a discrepancy between the actual and baseline matrix orders.

K>> actual

actual =

    35     1     6    26    19    24
     3    32     7    21    23    25
    31     9     2    22    27    20
     8    28    33    17    10    15
    30     5    34    12    14    16
     4    36    29    13    18    11

K>> data  = load(baseline)

data =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

If you run the verification in the Command Window with an appropriate actual value, the test passes.

K>> actual = magic(5);
K>> testCase.verifyEqualsBaseline(actual,baseline)

Exit debug mode and clear the breakpoint. Update the actual value in the test file, and save the file. If you run the test class, the baseline test passes.

result = runtests("ExampleTest");
Running ExampleTest
.
Done ExampleTest
__________

Version History

Introduced in R2024b