- Create and initialize the required structure variable in the base workspace.
- Save the structure variables from the base workspace to a .MAT file (let's call it data.mat)
- In the Simulink model, use the InitFcn model callback to load the data.mat file in the model workspace (So it is accessible across the model) - use load('data') command
- Now all the required structure variables are loaded in the model workspace and can be used across the model (there is no need to use assignin or evalin functions to make changes to variables across the workspaces as all the operations can be performed in model workspace itself) .
- Run the simulation as required (while making the use of the imported structure variables)
- If required, export the required variables to a MAT file- use save('myExportedData', var1, var2,... and so on) in StopFcn model callback where myExportedData.mat is the name of the exported .MAT file; var1, var2 are the variables to be exported.
How to update a structure in base workspace from a Simulink function block in every iteration?
21 views (last 30 days)
I have made a function block in simulink to act as an environment while training 3 RL agents. I store all the used variables and signals as arrays in a structure so that it is easy to import/export from/to workspce. I initialized all the arrays and variables with proper size in reset functions(eg. arrays that store signal values from each iteration have a length of simulation length in each episode.) and dummy inputs. I have taken this structure as a parameter input to this function (via add data) to get the base workspace structure variable that I initialised. I also have made the evalin function code.extrinsic and used in the function block to store that variable values in the structure because accessing a matfile would be too time consuming during simulation and assignin can not assign to structure variables (so will be this, but less I guess. No idea how else to do this). But everytime I run and try to store the signal values/ action values given by the RL agents for further calculations, even though the sizes are specified in the workspace for all fields in the structure, simulink throws an error "Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs." (same happens if I try to use assignin as well and so does using evalin to update the whole structure)
Can anyone please help me telling, a proper way to do this or where am I mistaken. (The code/model is simple but too big to attach.)
Nitin Kapgate on 8 Feb 2021
You cna refer to this article which provides possible workarounds for your problem.
Alternatively, in your case, you can follow the steps below:
To know more about different signal loading techniques in Simulink, refer the article here.
To know more about exporting (logging) simulation data from Simulink, refer the article here.