Variable declaration in matlab

570 views (last 30 days)
Is it necessary to declare a variable in matlab?

Accepted Answer

Walter Roberson
Walter Roberson on 5 Mar 2012
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".
  10 Comments

Sign in to comment.

More Answers (4)

Aldin
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
Oleg Komarov on 5 Mar 2012
What is a matrix variable?

Sign in to comment.


Shalini
Shalini on 5 Mar 2012
Can you give an example of declaration (integer/real) in a simple code?
  8 Comments
Mohashin Pathan
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

Sign in to comment.


KJDS SRINIVASA RAO
KJDS SRINIVASA RAO on 27 May 2013
yes sometimes required to remove confusion

Jon Camilleri
Jon Camilleri on 15 Nov 2015
So how do I initialize a variable and read the data types available?
  4 Comments
Walter Roberson
Walter Roberson on 25 Jun 2017
fun0 = inline('((x-5)^2+(y-5)^2-25)')
fun = inline('fun0(xy(1),xy(2))');
a = fminsearch(fun, [0 0])

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!