How do I create runtime objects for input ports?

조회 수: 4 (최근 30일)
Ninad
Ninad 2024년 9월 3일
답변: Samay Sagar 2024년 9월 16일
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
Rushikesh
Rushikesh 2024년 9월 4일
편집: Rushikesh 2024년 9월 4일
Ninad
Ninad 2024년 9월 4일
편집: Ninad 2024년 9월 4일
Yeah already done. " I am able to access the output data ports by creating listeners ". Now I want to create listeners for the input ports.

댓글을 달려면 로그인하십시오.

답변 (1개)

Samay Sagar
Samay Sagar 2024년 9월 16일
Hi @Ninad,
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:

카테고리

Help CenterFile Exchange에서 Signal Import and Export에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by