Akzeptierte Antwort

Walter Roberson
Walter Roberson am 5 Mär. 2012

4 Stimmen

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 Kommentare

Oleg Komarov
Oleg Komarov am 5 Mär. 2012
This is the clearest answer and I would stress that the most useful part of it to the level of Shalini's knowledge is the last sentence: "not usually".
An example of not usually is:
a = 2;
Altough, I am assigning a value of 2 to the variable named a, I have not declared the type of the variable which doesn't make it a C type declaration. The type is usually inferred and follows some default behaviour. In this case is double.
Also to draw a clear difference between the strict YES by Aldin, you cannot declare a variable WITHOUT assigning it some value (except for global variables).
Matt Tearle
Matt Tearle am 5 Mär. 2012
You can declare empty arrays though:
>> x = [];
>> y = cell(3,4);
Actually, y isn't empty... it just doesn't have anything in it! :)
Oleg Komarov
Oleg Komarov am 5 Mär. 2012
My statement: "you cannot declare a variable WITHOUT assigning it some value" is wrong for doubles and chars but if you have to declare somehting different you cannot do it directly, but you should convert, e.g. int32([]).
Same for cell, cell(1) creates cell with empty double.
In brief, usually there is no such a thing as EXPLICIT C type variable declaration in MATLAB.
Walter Roberson
Walter Roberson am 5 Mär. 2012
x = [];
would make x an empty array of type double precision (by default)
A global variable which has never been initialized with contain [], the empty double-precision array; the same is true for persistent variables which have not been initialized.
_Except_ for code generation, you can change the type of a variable at run time, including in the special cases I mention such as closures and shared variables and "poofing" in new values.
Jan
Jan am 5 Mär. 2012
@Oleg: x = zeros(0, 0, 'uint8') declares an empty UINT8.
c = cell(1) creates a cell, which contains a NULL pointer. Matlab interpretes it as empty DOUBLE.
Oleg Komarov
Oleg Komarov am 5 Mär. 2012
@Jan: I have not thought about zeros...
Just a curiosity, have you ever needed to declare an empty variable of type other than double?
Walter Roberson
Walter Roberson am 5 Mär. 2012
Yes, I have used empty structs with the appropriate fields.
Matt Tearle
Matt Tearle am 6 Mär. 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
James Tursa am 20 Dez. 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:
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Aldin
Aldin am 5 Mär. 2012

0 Stimmen

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 Kommentare

Oleg Komarov
Oleg Komarov am 5 Mär. 2012
I do not agree, because a proper declaration exists without assignment of a value.
Aldin
Aldin am 5 Mär. 2012
MATLAB works with only with matrix variable!!!
Oleg Komarov
Oleg Komarov am 5 Mär. 2012
What is a matrix variable?

Melden Sie sich an, um zu kommentieren.

Shalini
Shalini am 5 Mär. 2012

0 Stimmen

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

8 Kommentare

Aldin
Aldin am 5 Mär. 2012
first = input('Input first number: ');
second = input('Input second number: ');
% the result
result = first + second;
disp(result);
Aldin
Aldin am 5 Mär. 2012
Or so:
first = 2; % integer first of value "2"
second = 12; %integer second of value "2"
result = first + second; %result will be 14
disp(result);
Aldin
Aldin am 5 Mär. 2012
Press "Accept answer" if i was helpful
Walter Roberson
Walter Roberson am 5 Mär. 2012
Except for variables being used in code generation, variables are allowed to change type at any time.
first = 2; %a double precision (not integer!) assignment
second = int32(12); %a signed 32 bit integer assignment
first = second; %first will become a signed 32 bit integer!
second = {'hello'}; %second just became a cell array!
pavan
pavan am 20 Dez. 2012
Hey,Can u tell me for second=int32(12); whether 12 is assigned first in double precision and converted to int32? or it is int 32 right from the start?
Muruganandham Subramanian
Muruganandham Subramanian am 20 Dez. 2012
Bearbeitet: Muruganandham Subramanian am 20 Dez. 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
Walter Roberson am 20 Dez. 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
Mohashin Pathan am 27 Mai 2013
i want to initialize some double and some integer in a structure, can anyone help me how to do that

Melden Sie sich an, um zu kommentieren.

KJDS SRINIVASA RAO
KJDS SRINIVASA RAO am 27 Mai 2013

0 Stimmen

yes sometimes required to remove confusion
Jon Camilleri
Jon Camilleri am 15 Nov. 2015

0 Stimmen

So how do I initialize a variable and read the data types available?

4 Kommentare

Walter Roberson
Walter Roberson am 16 Nov. 2015
You initialize a variable by assigning a value to it.
You can informally find the data type by using whos() on the variable to see information about it. You can find its data type name in a program by asking for class() of the variable.
There is no complete list of data types available, because objects created through MATLAB Object Oriented Programming are considered distinct datatypes.
The "basic" data types are:
  • numeric: single(), double(), int8(), uint8(), int16(), uint16(), int32(), uint32(), int64(), uint64(). Each of these can also have a complex component
  • logical: logical(), false, true (can often be converted to numeric), cannot have complex component, stored as 1 byte each
  • char / string: char(), quoted strings, stored as 2 bytes each, using Unicode code points
  • cell array: cell()
  • structures: struct()
  • double() and logical() can also exist in sparse array form
ARUN BORGOHAIN
ARUN BORGOHAIN am 25 Jun. 2017
Bearbeitet: Walter Roberson am 25 Jun. 2017
I am using matlab_6.5 version;
v=[0 0]
x(1) = v(1);
y(1) = v(2);
fun = inline('((x-5)^2+(y-5)^2-25)')
a = fminsearch(fun, v )
% error giving(not enough input in fun???)
%---------------
fun = inline('((x(1)-5)^2+(x(2)-5)^2)')
a = fminsearch(fun, [0 0])
a =
4.9998 5.0001
%---------------------so how to dealare x,y;
It is fine with
x=x (1)& y=x (2)
Not with x & y
Stephen23
Stephen23 am 25 Jun. 2017
@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.
fun0 = inline('((x-5)^2+(y-5)^2-25)')
fun = inline('fun0(xy(1),xy(2))');
a = fminsearch(fun, [0 0])

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Variables finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by