How to pass an object instance from MATLAB to Simulink?

7 次查看(过去 30 天)
I would like to iteratively change the reward function used in an RL environment that I have created in Simulink. I would like to pass the function (which is a tree object) defined in MATLAB to Simulink and be able to use the associated methods (i.e. compute funcion) within the Simulink environment.
I would be appreciative for any advice in this regard.

回答(1 个)

Harsh Mahalwar
Harsh Mahalwar 2024-2-16
编辑:Harsh Mahalwar 2024-2-16
Hi Krupa,
From my understanding, you’re creating a RL environment in Simulink and trying to iteratively use a MATLAB function (the reward function), which is dependent on other MATLAB functions that you’ve created in your directory.
Here’s a possible solution, you can try using “iterator subsystems” to call your MATLAB function (reward function) iteratively,
(I am also using R2021a, so you can use these blocks as is)
In the above example, I have created a "while iterator subsystem” which runs n times (where n is the maximum number of iterations).
I have used a MATLAB function in this example which updates the values sent to its parameters iteratively.
function myCustomFunction()
disp("Message from the custom function")
end
(This is the code inside of the custom function)
function y = fcn(u)
disp("The value of iterator is " + u)
myCustomFunction()
y = u;
end
This is the code inside of the MATLAB function block and it uses a user-defined function from MATLAB. (when adding user defined functions in Simulink, do not forget the folder which consists of your functions to the MATLAB path)
(output for this model) As you can see, I was successfully able to run the custom function from inside of the Simulink.
Steps to add a folder to the MATLAB path:
1. Click on the “Set Path” button in the home tab >> environment section.
2. Click on Add Folder
3. Select the folder your MATLAB functions are in.
4. Click on save and voila! You have successfully added your functions to MATLAB path.
I hope this helps! Feel free to use the following links to learn more about “iterator subsystems”, “MATLAB function in SIMULINK” and “MATLAB path”.
Thanks!

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by