Matlab editor settings: notify undefined variables

52 views (last 30 days)
Matlab points out variables that are unused in my functions, which is great.
I would also like matlab to point out that some variables are not defined in a certain function. For example, if I have the code below, this code will bug, due to variable y not being defined. Is the matlab editor able to detect this? And if not, why not? Would that lead to undesired behaviour?
function [output] = myFunction(x)
x = 3*y
end

Accepted Answer

Ayush Anand
Ayush Anand on 16 Apr 2024 at 10:21
Hi,
MATLAB editor will not flag this like it flags unused variables. A couple of reasons I can think of are that the variable "y" might be defined in the base workspace which might be accessed during runtime and used. Also the variable "y" could have been declared global elsewhere which will not be a bug in the above context. If MATLAB can't find the variable during runtime, it will definitely throw an error but before compilation it will not flag it.
  3 Comments
Bruno Luong
Bruno Luong on 16 Apr 2024 at 10:51
Edited: Bruno Luong on 16 Apr 2024 at 10:55
I do not think both reasons are relevant :
  • vatable in base ws cannot be accessed without special commands (evalin, assign in)
  • global variable needs GLOBAL statement vefore ii enters in the scope.
More likely "Y" can be function name or class. So MATLAB editor in the current status cannot warn specially it could appears only after some ADDPATH ommand triggered during the runtime.
Bruno Luong
Bruno Luong on 16 Apr 2024 at 11:52
The variable name could also be puff in by a script like mfile called by the function. Or puffin by a subfunction that calls assignin('parent',...) or EVAL.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 16 Apr 2024 at 12:48
For one example of a scenario where a variable appears to be undefined, yet is properly used, consider the case of a nested function. NOT a sub-function, but a nested function. A nested function lives inside the caller function's workspace. And it can see the variables in that caller function, even though they are not passed in directly. For example, in the function nesttest, y is undefined, or so it would appear. But mytest calls nesttest. And since nesttest cannot see the variable y, which lives only in the caller workspace, and is NOT passed in, one would think an error would be generated when I call nesttest.
However, nesttest, since it is a nested function, has no problem seeing y.
mytest
x + y + 2 = 7
There are other problems with the idea of your code generating a warning, if a variable APPEARS to be undefined. For example, suppose you have a function where a variable is created on the fly, using eval? While this is an awful programming style to follow, it is legal syntax in MATLAB. (I wish it were not, but it is possible.) So, yes, I very well know eval is evil. But should MATLAB be able to know that the variable abc is defined here, before it is used?
eval([char([0 1 2] + 'a'),' = 3;'])
abc*2
ans = 6
function mytest()
x = 2;
y = 3;
nesttest(x)
function nesttest(x)
disp("x + y + 2 = " + (x + y + 2))
end
end
  2 Comments
Lennart Sinjorgo
Lennart Sinjorgo on 16 Apr 2024 at 13:24
'It is legal MATLAB syntax, therefore, the editor should not give a warning' is not an entirely valid argument. The editor also warns you if you write inv(A)*B instead of A\B. Moreover, the editor can also check first if a function is nested or not.
However, I know now that these warnings are currently not possible to enable, and probably never will. I'd think it would make for a good improvement though.
Steven Lord
Steven Lord on 16 Apr 2024 at 16:43
The editor also warns you if you write inv(A)*B instead of A\B.
Yes, Code Analyzer can detect calls of the form inv(A)*B through static analysis. If you were to "hide" that expression, something like:
A = randi([-10 10], 3);
B = sum(A, 2);
fh = @inv;
x = fh(A)*B;
Code Analyzer would not report that even though functionally that's the same as
x = inv(A)*B;
So the question is, can Code Analyzer prove that in this function:
function [output] = myFunction(x)
x = 3*y
end
looking solely at the code, that y is an undefined variable? No. What if there were a function named y (defined in another function file on the MATLAB path, though I'm defining it here in this Answers post for ease of illustration) that can accept 0 inputs and return 1 output?
function z = y()
z = pi;
end
That would allow this call to work.
myFunction(2)
x = 9.4248
3*pi
ans = 9.4248

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!