Variable declaration in matlab

조회 수: 50 (최근 30일)
Shalini
Shalini 2012년 3월 5일
댓글: Walter Roberson 2017년 6월 25일
Is it necessary to declare a variable in matlab?

채택된 답변

Walter Roberson
Walter Roberson 2012년 3월 5일
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
James Tursa
James Tursa 2012년 12월 20일
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:
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.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Aldin
Aldin 2012년 3월 5일
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
Aldin
Aldin 2012년 3월 5일
MATLAB works with only with matrix variable!!!
Oleg Komarov
Oleg Komarov 2012년 3월 5일
What is a matrix variable?

댓글을 달려면 로그인하십시오.


Shalini
Shalini 2012년 3월 5일
Can you give an example of declaration (integer/real) in a simple code?
  댓글 수: 8
Walter Roberson
Walter Roberson 2012년 12월 20일
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
Mohashin Pathan 2013년 5월 27일
i want to initialize some double and some integer in a structure, can anyone help me how to do that

댓글을 달려면 로그인하십시오.


KJDS SRINIVASA RAO
KJDS SRINIVASA RAO 2013년 5월 27일
yes sometimes required to remove confusion

Jon Camilleri
Jon Camilleri 2015년 11월 15일
So how do I initialize a variable and read the data types available?
  댓글 수: 4
Stephen23
Stephen23 2017년 6월 25일
@ARUN BORGOHAIN: please ask a new question. Note that your question has nothing to do with declaring variables: you need to ask about how to use fminsearch properly.
Walter Roberson
Walter Roberson 2017년 6월 25일
fun0 = inline('((x-5)^2+(y-5)^2-25)')
fun = inline('fun0(xy(1),xy(2))');
a = fminsearch(fun, [0 0])

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by