Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
Declaring variable size in Matlab Coder

Subject: Declaring variable size in Matlab Coder

From: Anders

Date: 2 Mar, 2012 11:05:12

Message: 1 of 4


I have an issue that keeps reoccurring when working with Matlab Coder. The problem is that Matlab does not know the size of my variables as manifested by the (:? x :?) in the Error report. When I try to use the assert statement before the variable is evaluated with assert(size(y,1)==1) or any other assert statement the variable is still evaluated as (:? x :?). I can not/do not wish to specify all variables in the command line as this is variables in functions within functions within functions.

My solutions to these problems is rather ugly at the moment. When working with scalars I define them as scalars by doing y = y(1) before the position that created an error. When working with a vector I am forced to use a for-loop in the same manner. But this can not have been the intentional usage. Are there any solutions to my problem, that allows a similar work flow as I have?

Subject: Declaring variable size in Matlab Coder

From: Anders

Date: 2 Mar, 2012 15:02:12

Message: 2 of 4

To clarify: I wish to - as an example - declare a fixed size in one dimension. By doing so removing the question mark :?

Subject: Declaring variable size in Matlab Coder

From: Anders

Date: 5 Mar, 2012 09:22:15

Message: 3 of 4


Subject: Declaring variable size in Matlab Coder

From: Mike Hosea

Date: 19 Mar, 2012 20:39:06

Message: 4 of 4

Anders wrote:
> Nobody?

Sorry. I don't read CSSM often.

Use coder.varsize to control variable sizing. For example, if you have
a variable-length row vector x, you can write


The second input is the shape, where "inf" is a proxy for "unknown upper
bound". If you know an upper bound, then you can use that that if you
want. The last argument is a logical array that indicates which
dimensions are variable. So [false,true] or [0,1] indicate that the
first dimension is fixed and the second varies.

For inputs to entry point functions, you can create proxies for inputs
with shapes like that. For example, if you have a function FOO that
accepts a variable x like that, you could write

xtype = coder.typeof(1,[1,inf],[false,true]);
codegen foo -args {xtype}

The first argument to coder.typeof determines the class. It has more
meaning in situations where the first argument is a struct. In a case
like this, all we get from it is class and complexity (i.e. whether the
data is real or complex). The second and third arguments are as in

The coder user interface makes it possible to define input types by
pointing and clicking.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us