Matlab editor settings: notify undefined variables
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Ayush Anand
2024-4-16
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 个评论
Bruno Luong
2024-4-16
编辑:Bruno Luong
2024-4-16
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
2024-4-16
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.
更多回答(1 个)
John D'Errico
2024-4-16
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
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
function mytest()
x = 2;
y = 3;
nesttest(x)
function nesttest(x)
disp("x + y + 2 = " + (x + y + 2))
end
end
2 个评论
Steven Lord
2024-4-16
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)
3*pi
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!