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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Variable declaration in matlab

Asked by Shalini on 5 Mar 2012

Is it necessary to declare a variable in matlab?

0 Comments

Shalini

Tags

Products

No products are associated with this question.

4 Answers

Answer by Walter Roberson on 5 Mar 2012
Accepted answer

There are some conditions under which it is necessary to initialize a variable ahead of time. These conditions have to do with "closures" and nested functions, and "poofing" variables.

Also, variables that will be used for with the code generator take their generated data type from the first assignment to the variable within the code.

Variables that are marked "global" must be declared as global before they are used; likewise with "persistent" variables.

There are no declarations such as in C or PASCAL, but there is enough leakage from the original "no declarations" model that the answer is more "not usually" than "no".

9 Comments

Walter Roberson on 5 Mar 2012

Yes, I have used empty structs with the appropriate fields.

Matt Tearle on 6 Mar 2012

@Oleg: yes, cell arrays. Behind the scenes, cell arrays are arrays of pointers to memory locations, so you suffer the same performance penalty if you don't preallocate them (even though you don't need to allocate what will go inside the cells). I've had situations where I've had large cell arrays, with each cell containing nothing particularly big, and I was filling the cells in with a loop.

(I'm interpreting "declare" in the MATLAB sense of x = cell(m,n);)

James Tursa on 20 Dec 2012

Sideways Comment: You can declare an unitialized numeric, char, or logical variable with a mex routine using unofficial API functions. E.g., see the UNINIT function from this FEX submission:

http://www.mathworks.com/matlabcentral/fileexchange/31362-uninit-create-an-uninitialized-variable-like-zeros-but-faster

Also, to clarify Jan's comment, when an unitialized cell or struct element is referenced (i.e., one with a NULL pointer behind the scenes), MATLAB will create a temporary empty 0x0 double matrix on the fly.

Walter Roberson
Answer by Aldin on 5 Mar 2012

Yes, when in you type in command window:

a = 2; it's a number (in JAVA: integer)
a = '2'; it's a string (in JAVA String)
a = [1 2 3 4 5]  it's a vector(array) (in JAVA: int array[] = new int[5])
a = [ 1 2; 3 4] it's a matrix (in JAVA: int array[][] = new int[2][2])
a = {'a',2;'b',3} it's a cell (in JAVA structure)

3 Comments

Oleg Komarov on 5 Mar 2012

I do not agree, because a proper declaration exists without assignment of a value.

Aldin on 5 Mar 2012

MATLAB works with only with matrix variable!!!

Oleg Komarov on 5 Mar 2012

What is a matrix variable?

Aldin
Answer by Shalini on 5 Mar 2012

Can you give an example of declaration (integer/real) in a simple code?

8 Comments

Muruganandham Subramanian on 20 Dec 2012

If the variable is already declared in other datatype,then it'll converts to int32()

e.g

a=2; % 'a' is double
b=int32(a) % 'b' is int32

else, it'll directly take datatype,which we mentioned or default

Walter Roberson on 20 Dec 2012

In older versions of MATLAB, int32(12) worked by having 12 evaluated in double precision first, creating a temporary (nameless) double precision variable. That temporary variable was then passed to the int32() routine, which did the conversion, creating a second temporary variable but of int32 type. That second temporary variable was returned from int32, after which the first temporary variable was deleted. The second temporary variable was then assigned to "second", which would happen by creating a permanent name for the nameless variable (not by copying its content, just naming it and holding on to it.)

In newer versions of MATLAB, int32() and similar numeric datatypes applied to constant numeric expressions of a very limited number of forms (e.g., uint64(134324342332432)) is handled at parsing time, special processing to ensure that the full precision is used; the parsing would directly create a temporary int32 variable. The assignment of that temporary variable to "second" would proceed like above, by assigning a permanent name to it rather than by copying it.

However, some testing I did a few weeks ago showed that if the numeric constant was not in one of a very limited number of formats, then even though it was numeric and constant, the special case would not be recognized, and instead the same sort of processing would be applied as for older MATLAB.

Mohashin Pathan on 27 May 2013

i want to initialize some double and some integer in a structure, can anyone help me how to do that

Shalini
Answer by KJDS SRINIVASA RAO on 27 May 2013

yes sometimes required to remove confusion

0 Comments

KJDS SRINIVASA RAO

Contact us