Discussion:
m files into Simulink
(too old to reply)
Elie Issa
2006-05-14 23:00:49 UTC
Permalink
Hello there,
I need to turn some matlab(.m) files into blocks in Simulink. I found
one solution(don't know if there's better) which is not working
properly; it consists of using Embedded Matlab Function Blocks.

Once this block is created and the code(.m) is transfered to the
embedded editor,i simulate the model(.mdl) but i get several errors:

-Undefined function or variable 'xyz'.
The first assignment to a local variable determines its class.
Inputs, outputs, and parameters inherit their class from the model
explorer.

-In an expression of the form start:stride:end, Embedded MATLAB only
supports constant strides.

PS: when simulating the m files in Matlab, none of these errors
appear.

I'd like to know if there's a better way to use m files in Simulink
or else what can i do to prevent these errors.

Thank you.
Fred Smith
2006-05-15 02:26:17 UTC
Permalink
Hi Elie,

The Embedded MATLAB function block only supports a subset of MATLAB (as you
found out) because it is intended for use with Real-Time Workshop. By
imposing these limitations the block is able to generate efficient C code.
If your eventual application involves generating C code from Smulink then
you will need to modify your M code to fit within the supported subset by
slogging through and fixing all these error messages.

However, if you don't care about C code generation, you can use the Embedded
MATLAB Function block to call your M-files directly. Here is how you do it.

If your external file is called foo.m and it returns two arguments (one a
2x4 double matrix, the other a scalar int8), you would write;
x = zeros(2,4);
y = int8(3);
[x,y] = foo(a,b
The first two lines inform Simulink what size and class of the outputs to
expects from your function. It will error during simulation if your
function returns outputs that do not conform to this specification.

Good luck.

-Fred

ps. There are some other possibilities if this approach doesn't work for
you. M-file S-functions give you complete flexibility but are a little bit
harder to write. The MATLAB Fcn block may also work in some limited cases.
Post by Elie Issa
Hello there,
I need to turn some matlab(.m) files into blocks in Simulink. I found
one solution(don't know if there's better) which is not working
properly; it consists of using Embedded Matlab Function Blocks.
Once this block is created and the code(.m) is transfered to the
-Undefined function or variable 'xyz'.
The first assignment to a local variable determines its class.
Inputs, outputs, and parameters inherit their class from the model
explorer.
-In an expression of the form start:stride:end, Embedded MATLAB only
supports constant strides.
PS: when simulating the m files in Matlab, none of these errors
appear.
I'd like to know if there's a better way to use m files in Simulink
or else what can i do to prevent these errors.
Thank you.
Loading...