Hi Jiayi,
Yes, you can use the "sim" function in MATLAB to interact with an experimental environment when you're working on reinforcement learning or custom training loops. The "sim" function allows you to simulate the agent's interaction with the environment and collect data like "observations", "actions", "rewards", and terminal states ("isDone").
Your code snippet looks good for storing experiences in a replay buffer:
out = sim(agent, env); % Runs the sim.
% Store observations, actions, rewards, and isDone flags in myBuffer.
Make sure your buffer size and indexing (1:myBuffer.bufferSize-1 and 2:myBuffer.bufferSize) are set correctly to avoid indexing errors. Also, ensure that your environment and agent are properly defined to provide the expected data structure.
To learn more about the "sim" function, you can refer to the following documentation link: