Contenido principal

Access Data in MATLAB Function During Simulation

You can stream signal data to a MATLAB® callback function during simulation using the Data Access options in the Instrumentation Properties for a signal. To open the Instrumentation Properties dialog box, right-click the logging badge on a signal that is marked for logging and select Properties. You can also configure or access the data access settings programmatically using the Simulink.sdi.setDataAccess function and the Simulink.sdi.getDataAccess function.

The function you provide receives data in packets asynchronously throughout the simulation. The callback function executes each time it receives new data. You can write a callback function to process signal data during simulation or to create a custom visualization of a signal. The callback function does not affect signal values in your model. This example illustrates the steps required to access signal data during simulation using a simple callback function that plots the signal.

Note

Data access only supports 1-D and 2-D matrix signals and does not support Rapid Accelerator mode, referenced models, or virtual bus signals.

Before R2025a: Data access does not support fixed-point data.

Write Callback Function for Data Access

The data access callback function always receives signal data as the first argument. You can choose whether to send the simulation time and a parameter. When you include all three arguments, simulation time is the second argument. Your callback function can be specific to a single signal, or you can use the same callback to process and visualize multiple signals. The callback function only has access to data for a single signal at a time. This example creates a callback function that receives signal data, time, and a parameter used to identify which signal the function is processing for a given call.

Note

The data access callback function receives nonvirtual bus data as a structure of arrays that matches the hierarchy of the bus.

Author your callback function in an .m file with the same name as your function. For more information about writing MATLAB functions, see Create Functions in Files. The example callback function uses the optional parameter to assign a numerical identifier to each signal. The parameter is used to create a unique figure for each signal and assign each signal a different line color. To accumulate signal data on the plot, the callback includes hold on. For each call to the callback, the function receives a portion of the signal data. You can use the callback to accumulate the packets if desired.

function plotSignals(y,time,sigNum)
   
    figure(sigNum)
    
    if isequal(sigNum,1)
        marker = "ro-";
    elseif isequal(sigNum,2)
        marker = "go-";
    else
        marker = "bo-";
    end
    
    hold on;
    plot(time,y,marker);

end

The callback function provides no return values. If you specify the function with returns, the return data is discarded.

Tip

Use assignin to access data from the callback function in the base workspace.

Save the callback function in a location on the MATLAB path. If the location where you save the callback is not on the path, you can add it to the path. Right-click the directory containing the callback in the Current Folder section in MATLAB and select Add to Path.

Configure Signals for Data Access

The ThreeSigs model logs output data from a Sine Wave, Random Number, and Chirp Signal block. Configure signals in the ThreeSigs model to use the callback from the previous section.

Open the ThreeSigs model.

mdl = "ThreeSigs";
open_system(mdl)

To access data for a signal with the data access callback, you must use signal logging. To mark the sinSig, randSig, and chirpSig signals for logging, click and drag to select the three signals and click Log Signals.

Alternatively, you can use Simulink.sdi.markSignalForStreaming to mark the signals for signal logging.

lines = get_param(mdl,"Lines");
sine_handle = lines(3).Handle;
rand_handle = lines(2).Handle;
chirp_handle = lines(1).Handle;

Simulink.sdi.markSignalForStreaming(sine_handle,"on")
Simulink.sdi.markSignalForStreaming(rand_handle,"on")
Simulink.sdi.markSignalForStreaming(chirp_handle,"on")

To configure data access for the each signal, right-click the logging badge and select Properties. In the Instrumentation Properties dialog box, on the Data Access tab, you can specify the name of your callback function, whether the callback function takes time as an argument, and the optional parameter value.

For this example, in the Instrumentation Properties dialog box, on the Data Access tab:

  1. Select Enable Run-Time Data Access.

  2. In the Function name text box, enter the name of the callback, plotSignals.

  3. Select Include simulation time.

  4. In the Function parameter text box, enter 1 for sineSig, 2 for randSig, and 3 for chirpSig.

The Instrumentation Properties for the z, rad/sec signal

Simulate the model. The callback generates Figures 1, 2, and 3 to display sineSig, randSig, and chirpSig during simulation.

out = sim(mdl);

The sinSig, randSig, and chirpSig signals plotted with the line styles specified in the plotStyles function.

You can modify the callback function to create a custom visualization or to create a plot of processed signal data. Errors related to the data access callback function do not interrupt simulation. The errors surface in the Diagnostic Viewer as a warning.

See Also

Tools

Functions

Topics