How to access methode from class
2 views (last 30 days)
Show older comments
Hi all
I'm new to Matlab/Simulink. See image below, how do I access a method from the instance 'fbTriggerBuffer' of the class 'FB_TriggerBuffer' in simulink? In code I would write something like
returnValue = fbTriggerBuffer.mAddRecord(i_fActXposition);
'mAddRecord' is the method I want to call, everytime when the input 1 has a rising edge.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1515379/image.png)
Thanks for any reactions.
Ludo
0 Comments
Answers (1)
Abhinaya Kennedy
on 7 Nov 2023
Hello Ludo,
I understand you want to access a method from an instance of a class in Simulink. To do so, you can use a MATLAB Function block. Here's how you can call the `mAddRecord` method from the instance `fbTriggerBuffer` of the class `FB_TriggerBuffer`:
1. Open your Simulink model and locate the location where you want to call the method.
2. Add a MATLAB Function block to your model. You can find it in the Simulink Library Browser under "User-Defined Functions" or by searching for "MATLAB Function" in the search bar.
3. Double-click on the MATLAB Function block to open the block editor.
4. In the block editor, write the following code to call the method:
function y = fcn(u)
persistent fbTriggerBuffer; % Declare the persistent variable
if isempty(fbTriggerBuffer) % Create the instance on the first call
fbTriggerBuffer = FB_TriggerBuffer(); % Create the instance
end
if u(1) > 0 && u(1) < 1 % Check for rising edge on input 1
returnValue = fbTriggerBuffer.mAddRecord(u(2)); % Call the method
end
y = u; % Pass the input to the output
end
Make sure to replace `FB_TriggerBuffer` with the actual class name, and `u(2)` with the appropriate input argument for the `mAddRecord` method.
5. Save and close the MATLAB Function block.
6. Connect the input and output ports of the MATLAB Function block to the rest of your Simulink model as needed.
Now, whenever the input 1 of the MATLAB Function block has a rising edge (i.e., goes from 0 to 1), the `mAddRecord` method will be called with the appropriate argument.
Hope this helps!
0 Comments
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!