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")
(This is the code inside of the custom function)
disp("The value of iterator is " + u)
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!