Contenido principal

Simulink.sdi.setDataAccess

Specify settings for streaming signal data to MATLAB callback function during simulation

Since R2026a

    Description

    dataAccess = Simulink.sdi.setDataAccess(block,portIndex,Name=Value) sets the data access settings for a signal identified by the block path or handle and the port index using one or more name-value arguments. For example, to specify the name of the data access callback function that processes or visualizes signal data during simulation, set CallbackFunction to the name of that callback function, such as "myCallback".

    example

    dataAccess = Simulink.sdi.setDataAccess(portHandle,Name=Value) sets the data access settings for a signal specified using the output port handle.

    example

    dataAccess = Simulink.sdi.setDataAccess(lineHandle,Name=Value) sets the data access settings for a signal specified using the line handle.

    example

    Examples

    collapse all

    Mark the signal connected to the first output port of the Gain block in the model myModel for logging.

    Simulink.sdi.markSignalForStreaming("myModel/Gain",1,"on");

    To configure data access settings for the logged signal, use the Simulink.sdi.setDataAccess function with the block path and port index of the source block. For example, specify these settings:

    • Enable streaming signal data to a MATLAB® function during simulation.

    • Use the callback function named myCallback as the MATLAB function that receives signal data during simulation.

    • Specify a function parameter of "1" to identify which signal data the function receives.

    • Include simulation time.

    Simulink.sdi.setDataAccess("myModel/Gain",1,Enable=true,...
        CallbackFunction="myCallback",FunctionParameter="1",IncludeTime=true)

    View the data access settings using the Simulink.sdi.getDataAccess function.

    dataAccess = Simulink.sdi.getDataAccess("myModel/Gain",1)
    dataAccess = struct with fields:
                   Enable: 1
         CallbackFunction: 'myCallback'
        FunctionParameter: '1'
              IncludeTime: 1

    Use the get_param function to get the port handles for the source block of your signal of interest. For example, get the port handle for the output port of the Gain block in the model myModel.

    portHandles = get_param("myModel/Gain","PortHandles");
    myPortHandle = portHandles.Outport;

    Use the port handle to mark the desired signal for logging.

    Simulink.sdi.markSignalForStreaming(myPortHandle,"on");

    To configure data access settings for the logged signal, use the Simulink.sdi.setDataAccess function with the port handle of the source block. For example, specify these settings:

    • Enable streaming signal data to a MATLAB function during simulation.

    • Use the callback function named myCallback as the MATLAB function that receives signal data during simulation.

    • Specify a function parameter of "1" to identify which signal data the function receives.

    • Include simulation time.

    Simulink.sdi.setDataAccess(myPortHandle,Enable=true,...
        CallbackFunction="myCallback",FunctionParameter="1",IncludeTime=true)

    View the data access settings using the Simulink.sdi.getDataAccess function.

    dataAccess = Simulink.sdi.getDataAccess(myPortHandle)
    dataAccess = struct with fields:
                   Enable: 1
         CallbackFunction: 'myCallback'
        FunctionParameter: '1'
              IncludeTime: 1

    Use the get_param function to get the line handle for your signal of interest. For example, get the handle for the signal line connected to the output port of the Gain block in the model myModel.

    lineHandles = get_param("myModel/Gain","LineHandles");
    myLineHandle = lineHandles.Outport;

    Use the line handle to mark the desired signal for logging.

    Simulink.sdi.markSignalForStreaming(myLineHandle,"on");

    To configure data access settings for the logged signal, use the Simulink.sdi.setDataAccess function with the line handle. For example, specify these settings:

    • Enable streaming signal data to a MATLAB function during simulation.

    • Use the callback function named myCallback as the MATLAB function that receives signal data during simulation.

    • Specify a function parameter of "1" to identify which signal data the function receives.

    • Include simulation time.

    Simulink.sdi.setDataAccess(myLineHandle,Enable=true,...
        CallbackFunction="myCallback",FunctionParameter="1",IncludeTime=true)

    View the data access settings using the Simulink.sdi.getDataAccess function.

    dataAccess = Simulink.sdi.getDataAccess(myLineHandle)
    dataAccess = struct with fields:
                   Enable: 1
         CallbackFunction: 'myCallback'
        FunctionParameter: '1'
              IncludeTime: 1

    Input Arguments

    collapse all

    Source block path or handle for the block with the desired signal connected to one of its output ports, specified as a string or character vector. To get the path or handle of the currently selected block, use the gcb or gcbh function, respectively.

    Example: "myModel/Gain"

    Output port index of the source block connected to the desired signal, specified as a positive integer.

    Example: 1

    Port handle of the output port of the source block connected to the signal of interest, specified as a handle.

    Use the get_param function with the "PortHandles" option to get port handles.

    Example: myPortHandles.Outport(1)

    Line handle for the signal of interest, specified as a handle.

    Use the get_param function with the "LineHandles" option to get line handles.

    Example: myLineHandles.Outport(1)

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: Simulink.sdi.setDataAccess("myModel/Gain",1,Enable=true,CallbackFuntion="myCallback",FunctionParameter="1",IncludeTime=true)

    Option to stream signal data to MATLAB function during simulation, specified as true or false.

    When enabled, you can specify a data access callback function that processes or visualizes the logged signal data during simulation. The function receives data in packets asynchronously throughout the simulation, and the function executes each time it receives new data. For more information, see Access Data in MATLAB Function During Simulation.

    To enable the option to stream signal data to a MATLAB function, you must also provide a valid MATLAB function using the CallbackFunction name-value argument.

    Example: Simulink.sdi.setDataAccess("myModel/Gain",1,Enable=true,CallbackFuntion="myCallback")

    MATLAB function that receives signal data during simulation, specified as a valid MATLAB function name.

    The data access callback function always receives signal data as the first argument and may also receive simulation time and a function parameter as additional arguments. When the function receives all three arguments, simulation time is the second argument. For more information, see Access Data in MATLAB Function During Simulation.

    For more information about writing MATLAB functions, see Create Functions in Files.

    To enable streaming signal data to a MATLAB function during simulation, specify the Enable parameter as true.

    Example: Simulink.sdi.setDataAccess("myModel/Gain",1,CallbackFuntion="myCallback")

    Option to use parameter to identify data source, specified as a string, character vector, or numeric value.

    When you specify a value for the Function parameter, the data access callback receives the specified value as a second or third argument depending on whether time is included with data sent to the callback function. When you use the same data access callback function to process data for more than one signal, specify a function parameter to identify which signal data the function receives in a given call. For more information, see Access Data in MATLAB Function During Simulation.

    To enable streaming signal data to a MATLAB function during simulation, specify the Enable parameter as true.

    Example: Simulink.sdi.setDataAccess("myModel/Gain",1,FunctionParameter="2")

    Option to send simulation time to MATLAB function, specified as true or false. The data access callback function always receives signal data as the first argument and may also receive simulation time and a function parameter as additional arguments. When a function receives all three arguments, simulation time is the second argument. For more information, see Access Data in MATLAB Function During Simulation.

    To enable streaming signal data to a MATLAB function during simulation, specify the Enable parameter as true.

    Example: Simulink.sdi.setDataAccess("myModel/Gain",1,IncludeTime=true)

    Version History

    Introduced in R2026a