Writing Workspace with M file

1 view (last 30 days)
Kevin
Kevin on 28 Mar 2012
So, i'm working on a code-generation project, wich is intended to be used by people who aren't used to work with Matlab/Simulink.
When using signals/parameters in the project, these parameters/signals need to be ASAP2 defined, but i want to make this easier for the people who have to work with it by inserting blocks into my custom library of simulink, that will define the parameters/signals for them, while they define the parameters (datatype, storageclass, min/max values) in the block mask.
My question is, is there a way to let an M file (attached to the simulink block) write defining commands (name = ASAP2.Signal; etc) into the matlab workspace?

Answers (1)

Kaustubha Govind
Kaustubha Govind on 28 Mar 2012
You can write your MATLAB code in Block Callback Functions. You can put it in LoadFcn if you want the commands to run only once at model load time, or in InitFcn if you want them to run every time the user updates (Ctrl+D) or runs (Ctrl+T) the model.
  2 Comments
Kevin
Kevin on 29 Mar 2012
I Think we are thinking on the wrong points here. The functions you've linked me are to interrupt a simulation, to execute some other functions. But i'm not working with the simulation functions, only with code generation. My intention is that when the user starts the code generation process (Ctrl+B), when the block is then evaluated, i want it to place commands into the matlab workspace (ex. tempSignal = ASAP2.Signal; tempSignal.DataType = 'uint16'; tempSignal.StorageClass = 'ExportedGlobal; etc), or at least define the used signals and parameters (if there would be any other option to do this, as i'm not sure these commands can be executed during the building process). This has to happen just before matlab starts writing the C code for my model, or else the code will give errors when compiling the code into a HEX file.
Kaustubha Govind
Kaustubha Govind on 30 Mar 2012
Kevin: LoadFcn is run as soon as the model is loaded, regardless of whether you run simulation/code-generation or do nothing at all. AFAIK, the InitFcn is run every time the model is updated - this happens every time both before simulation and code-generation.
Did you want to create the variables *only* for code-generation, but never for simulation? If yes, you may use a slightly more advanced concept of a hook file. See http://www.mathworks.com/help/toolbox/rtw/ug/f10435.html
You can try writing your code in the 'entry' hook and see if that works.

Sign in to comment.

Categories

Find more on Modeling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!