How to pass an object instance from MATLAB to Simulink?
8 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Harsh Mahalwar
on 16 Feb 2024
Edited: Harsh Mahalwar
on 16 Feb 2024
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!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!