Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using argument in embedded matlab function to create array

362 views
Skip to first unread message

Thomas Marullo

unread,
Dec 18, 2008, 12:57:04 PM12/18/08
to
I have an Embedded MATLAB function which takes in two arguments:
Constant: constantVar
Nx1 Array: arrayVar

When I try to run this command,
myArray = zeros(constantVar, 1);

I get the following error:
Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression

What I am confused about is that in the Simulink model, the inport of the EMF block has a constant block.

However, if I run the command,
for i = 1:length(arrayVar)
...some code...
end
It works fine. I would have thought I'd get the same error. I assume for loops have different requirements than the zeros function.

Is there something I'm missing here to have the zeros function work with an argument?

Michael Hosea

unread,
Dec 18, 2008, 1:58:40 PM12/18/08
to
First of all, I agree that this is confusing. Input signals in Simulink are
always considered variable by an Embedded MATLAB block, possibly by any
block, but I'm not a Simulink expert, so I'm not entirely sure. The
following is my current understanding. A so-called "constant block" simply
produces the same value at every time step. Thus, one *think* of it as
constant, but the signal itself that propagates forward doesn't have this
property of being constant attached to it. That is to say, as far as the
rest of the model is concerned, a constant block just produces a variable
signal like any other.

The way to do what you want is with a non-tunable parameter, not with a
signal. Get rid of the constant block. Then bring up the Model Explorer.
Find the Embedded MATLAB block in the model hierarchy on the left. Click on
it, and then click on the input argument that you need to be a constant in
the middle pane (or right click on it and select "Properties" to bring up
the properties dialog). Then over in the right pane (if you left-clicked)
or properties dialog (if you right-clicked), change the "Scope" from "Input"
to "Parameter" and uncheck the "Tunable" box that appears after you change
the scope. Apply those changes. Now define the named variable in your
MATLAB workspace to have the constant value you want. Rebuild and run the
simulation. Now you should be able to change this value, rebuild, and run
the simulation without changing the model.
--
Mike

"Thomas Marullo" <tm...@lehigh.edu> wrote in message
news:gie2tg$kg8$1...@fred.mathworks.com...

Thomas Marullo

unread,
Dec 18, 2008, 3:09:02 PM12/18/08
to
Firstly, thank you for explaining the differences in the way Simulink and EML interpret signals. And secondly, your method worked. Thanks!

Ahmed Boujelbene

unread,
Feb 24, 2012, 11:23:12 AM2/24/12
to
Hi,
my problem seem to be the same as you but with a variable PAD_bit in the code that have value according to the condition if..else.
if i make zeros(1,PAD_bit)
it returns me: non-constant expression or empty matrix. If the expression is used to
specify the size of some other expression, you may also consider enabling
variable-size arrays in the ports and data manager dialog of the MATLAB
Function block (#29). If the expression is used to specify the class of
some other expression, it must be constant.

Function 'MATLAB Function' (#29.916.923), line 37, column 7:
"PAD_bit"
Please, I need a help.
0 new messages