Why do I get an error in MATLAB 6.0 (R12) where I got a warning in MATLAB R11 with reference to uninitialized variables?
1 view (last 30 days)
Show older comments
I execute the following code:
a = [a,3];
In MATLAB 5.3 (R11) I received the following warning:
Warning: Reference to uninitialized variable a.
In MATLAB 6.0 (R12) I get:
??? Undefined function or variable 'a'.
Accepted Answer
MathWorks Support Team
on 20 Jan 2010
There are actually three cases to be considered with reference to uninitialized variables in MATLAB.
The first case is the one listed above where is a variable is used on the right hand side before being initialized.
function before1
a = [a,3];
MATLAB 5.3 (R11)
>> before1
Warning: Reference to uninitialized variable a in before1 at line 2.
> In d:\temp\before1.m at line 2
MATLAB 6.x (R12.x)
>> before1
??? Undefined function or variable 'a'.
Error in ==> d:\temp\before1.m
On line 2 ==> a = [a,3];
MATLAB 6.5 (R13)
>> before1
??? Undefined function or variable 'a'.
Error in ==> d:\temp\before1.m
On line 2 ==> a = [a,3];
The second case is where a variable is used on the right hand side after it first appeared on the left hand side of an assignment which was not executed. For example:
function before2
if 0
a = 1;
end
a = [a,3];
MATLAB 5.3 (R11)
>> before2
Warning: Reference to uninitialized variable a in before2 at line 5.
> In d:\temp\before2.m at line 5
MATLAB 6.x (R12.x)
>> before2
Warning: Reference to uninitialized variable a in before2 at line 5.
> In d:\temp\before2.m at line 5
MATLAB 6.5 (R13)
>> before2
??? Undefined function or variable 'a'.
Error in ==> d:\temp\before2.m
On line 5 ==> a = [a,3];
The third case is called undefined. It occurs when a variable first appears on the right-hand side of an assignment which was not yet executed. For example,
function before3
if 0
z = a;
end
a = [a,3];
MATLAB 5.3 (R11)
>> before3
??? Undefined function or variable 'a'.
Error in ==> d:\temp\before3.m
On line 5 ==> a = [a,3];
MATLAB 6.x (R12.x)
>> before3
??? Undefined function or variable 'a'.
Error in ==> d:\temp\before3.m
On line 5 ==> a = [a,3];
MATLAB 6.5 (R13)
>> before3
??? Undefined function or variable 'a'.
Error in ==> d:\temp\before3.m
On line 5 ==> a = [a,3];
The finalized behavior for this issue is that using an uninitialized variable is always an error.
There are various ways to deal with this new behavior. One alternative is to set the variable in question to empty at the beginning of the program. Since uninitialized variables were implicitly treated as empty matrices, this will preserve the semantics of the program. However, a better second-order fix would be to determine why the variable is being used before being assigned and correct the discrepancy.
An example:
function out = before(in)
% x and out are undefined if in >= 0
if (in < 0)
x = -in;
out = x;
end
y = ones(x);
Another fix would be:
function out = before(in)
if (in < 0)
x = -in;
out = x;
else
x = in;
if nargout > 0
error('Too many outputs requested for positive value of IN');
end
end
y = ones(x);
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!