How to access methode from class

1 回表示 (過去 30 日間)
Ludo Houben
Ludo Houben 2023 年 10 月 19 日
回答済み: Abhinaya Kennedy 2023 年 11 月 7 日
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.
Thanks for any reactions.
Ludo

回答 (1 件)

Abhinaya Kennedy
Abhinaya Kennedy 2023 年 11 月 7 日
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!

カテゴリ

Help Center および File ExchangeSimulink についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by