MATLAB: How do i run a Simulink simulation from within a Matlab function block in a Simulink simulation

extrinsicnested simulation

So I have a Quadcopter model which follows a given path. In the control loop for this quadcopter i have a matlab function block which is suppose to run a simulation of quadcopter to simulate the next 2 seconds of flight, given the current state and return some data from this simulation to be used in the control loop output. The problem i was having was that I cant seem to get any data into the parent workspace to use in the control loop. I was able to map the state to the subsimulation but I had to declare the 'run' and 'sim' commands as coder.extrinsic for them to run else I get code generation errors. As far as my understanding goes once they are declared extrinsic Matlab doesnt allow you to return data from the simulation by definition.
My question is basically how do I get data from a simulation after declaring the sim and run commands extrinsic? I have tried to use Data stores and setting return data in the simulation configuration but nothing worked so far. Sorry if the post is not clear, first time posting in a forum.

Best Answer

For running models in a parent model, I strongly suggest you to use Model Referencing. Please see the following link for details:
Do not try to use sim or run commands.
Build your MATLAB Function model in a new another model so that it will be easier and more reliable for you to manage ongoing situations.