How do I create runtime objects for input ports?
4 views (last 30 days)
Show older comments
Hey I wanted to create runtime objects to access data flowing from input ports to my simulink model. I am able to access the output data ports by creating listeners. I was wondering if I could use the "PreInputs" event to create listeners for my input ports. Pleas help me with the code, I am getting errors as I am unable to specify the path to my block. Also I want to plot the data received vs Simulation time in my gui . Any suggestions?! Cheers !
2 Comments
Answers (1)
Samay Sagar
on 16 Sep 2024
I see that you are trying to create event listeners for “inports” in Simulink. However as mentioned in the Mathworks documentaion here https://www.mathworks.com/help/releases/R2024a/simulink/ug/accessing-block-data-during-simulation.html , only non virtual blocks can have runtime objects. Since “inport” is a virtual block, it does not have an associated runtime object. Hence you cannot directly create an event listerner for "inports".
However you can employ the following workaround to monitor input data. You can place a non-virtual block, such as a "Gain" block with a gain of 1, directly after each "Inport" to convert the signal into a form that supports runtime objects. Thereafter, you can create event listeners for this block as per your use case.
Here is how you can implement this event listener to plot input data:
function createEventListener()
% Path to the block
blockPath = 'sample1/Gain';
% Add event listener for PreOutputs (once simulation starts)
lh = add_exec_event_listener(blockPath, 'PreOutputs', @captureInputData);
% Store listener in base workspace if needed to prevent it from being cleared
assignin('base', 'listenerHandle', lh);
end
function captureInputData(block, ~)
% Access the input signal at runtime
inputSignal = block.InputPort(1).Data; % Assuming 1 input port
disp(inputSignal); % Display the data for testing
% Plot the data (store it in variables and update a plot)
global inputData timeData;
inputData = [inputData, inputSignal];
simTime = get_param(bdroot, 'SimulationTime');
timeData = [timeData, simTime];
plot(timeData, inputData);
end
Once you have defined the above functions, you can add the event listener by calling the createEventListener function in the model's "StartFcn" callback.
If your goal is only to plot a graph between input data and time, you can use the “To Workspace” block to log the input signal data and plot its graph or connect a scope block to the input signal to display its data during simulation.
For more information about "Virtual and Non Virtual Blocks", you can refer the following documetation:
You can check the events supported by the listener here:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!