Running Previously User-Built MATLAB code from Simulink

1 view (last 30 days)
I have created a function wrapper (interval_sim_callv4())around MATPOWER's functions all coded through MATLAB. I also created a function (GrabSysElements())in MATLAB to find my user-defined masked block types, how many there are, and their parameters a user has built in simulink using find_system() and get_param().
I want to run these m-files I created in MATLAB after a user builds a system of blocks and sets their parameters. These parameters will be used to create another m-file with a data structure to give parameters in the format MATPOWER uses.
How can I get around the coder.extrinsic strategy so I can avoid changing literally over 100 files of code which also use local variables and such which Embedded MATLAB is not capable of working with? Many of these variables are dynamic and change based upon a user's new creation of a simulink model.

Answers (4)

Fred Smith
Fred Smith on 6 Jul 2011
Hi Andrew,
Could you please provide a bit more detail?
I am puzzled by how coder.extrinsic became involved in this? What is your goal? Are you changing the behavior of the Simulink model? Are you generating code from MATLAB? Is your goal acceleration or code generation? What kinds of functions are you using that you need to put coder.extrinsic everywhere? Why do you say that Embedded MATLAB cannot handle local variables? That isn't true. When you say that the variables change, when do they change?
As you can see I have lots of questions.
Thanks,
Fred

Fred Smith
Fred Smith on 6 Jul 2011
Hi Andrew,
Could you please provide a bit more detail?
I am puzzled by how coder.extrinsic became involved in this? What is your goal? Are you changing the behavior of the Simulink model? Are you generating code from MATLAB? Is your goal acceleration or code generation? What kinds of functions are you using that you need to put coder.extrinsic everywhere? Why do you say that Embedded MATLAB cannot handle local variables? That isn't true. When you say that the variables change, when do they change?
As you can see I have lots of questions.
Thanks,
Fred

Andrew
Andrew on 6 Jul 2011
Ive answered my own question by using the run command
Using the 'run' command through the Embedded MATLAB block in simulink I just called the function (GrabSysElements)to be ran outside of Simulink so that I could avoid labeling functions as extrinsic.
Using the play button in Simulink after creating a model of a power grid using generators, buses, and branches, I would press the play button. I had built a function (GrabSysElements.m) to read each block in the model and retrieve each parameter I created via a Edit Mask. I called my GrabSysElements function in the Embedded Matlab Function block that was also in the model in hopes that it would just use the function with no problems.
However, I realized also that the variables that I have in my function GrabSysElements were being sized at each run of a for loop based on the locating of a specific type of masked block I created. Since a user may come up with any number of several unique blocks I've provided, these variable arrays will be of different sizes for each simulation. This was my problem.
For future reference though, it seems to me that Embedded MATLAB code isn't meant to be lengthy. How would one declare variables which their size changes based on some algorithm someone writes to find the length of the variable array?
  1 Comment
Alexander Bottema
Alexander Bottema on 7 Jul 2011
You can use 'coder.varsize(...)' to declare a variable to be of variable size. For the MATLAB function block in Simulink (formerly known as the Embedded MATLAB function block) you have to specify the maximum size on how big this array can be. To get the length of an array you could extract the sizes for all dimensions (size(x)) or the total number of elements (numel(x)).
Another option to create variable size arrays is to put value constraints when calling a matrix constructor (zeros/ones/repmat).
For example,
% ...n is some input from a function...
assert(n <= 10);
x = zeros(1,n); % create a matrix 1xN where N is at most 10.

Sign in to comment.


Andrew
Andrew on 6 Jul 2011
I may not have been clear on my answer but by using the 'run' command I only had to label 'run' as extrinsic and GrabSysElements I did not have to worry about.
Andrew

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!