You can stream signal data to a MATLAB® callback function during simulation using the Data Access functionality in the Instrumentation Properties for a signal. 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.
Data access does not support Rapid Accelerator mode, referenced models, fixed-point data, or virtual bus signals and only supports 1-D and 2-D matrix signals.
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.
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.
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.
To access data for a signal with the data access callback, you need to log the signal. To mark a signal for logging, right-click the signal in your model and select Log Selected Signals. To configure data access for the signal, right-click the logging badge and select Properties. On the Data Access tab of the Instrumentation Properties, you can specify the name of your callback function, whether the callback function takes time as an argument, and the optional parameter value.
For example, open the
vdp model and configure the
x2 signals to use the callback from the
vdp in the MATLAB
x2 signals. Then,
right-click the selected signals and choose Log Selected
Signals from the context menu.
To open the Instrumentation Properties, right-click the
logging badge for
x1 and select
On the Data Access pane, enter the name of the callback,
check Include simulation time, and enter
the Function parameter.
Open the Instrumentation Properties for
and configure the Data Access pane to use the
plotSignals callback with a Function
parameter value of
vdp model. The callback generates Figures 1 and 2 to
x2 signals during
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.