MATLAB Answers

How to update a structure in base workspace from a Simulink function block in every iteration?

6 views (last 30 days)
Sunandan Adhikary
Sunandan Adhikary on 5 Feb 2021
Edited: Sunandan Adhikary on 9 Feb 2021
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.)

Answers (1)

Nitin Kapgate
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:
  1. Create and initialize the required structure variable in the base workspace.
  2. Save the structure variables from the base workspace to a .MAT file (let's call it data.mat)
  3. 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
  4. 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) .
  5. Run the simulation as required (while making the use of the imported structure variables)
  6. 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.
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.
  1 Comment
Sunandan Adhikary
Sunandan Adhikary on 9 Feb 2021
Tried it. Not working. May be I am missing something here.
saved after initializing in the base workspace, in the reset function : save('system.mat','-struct','s')
[without having the '-struct' param, it did not dump structure fields in the mat file and simulink doesn't recognize the fields while updation!]
s= load('system') in MOdel Properties-->InitFcn tab
save('system.mat',''-struct',s') in Model Properties--> StopFcn tab
[Simulation stops without any error at the start of 2nd episode if not using the same '-struct']
Does not show any error but I can not see any s variable created in model workspace and the previous iteration data doesn't get printed(same as before, only initialised data and data from current iteration is printed) while I am printing fields of s inside the function.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!