Borrar filtros
Borrar filtros

Matlab editor settings: notify undefined variables

7 visualizaciones (últimos 30 días)
Lennart Sinjorgo
Lennart Sinjorgo el 16 de Abr. de 2024
Comentada: Steven Lord el 16 de Abr. de 2024
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

Respuesta aceptada

Ayush Anand
Ayush Anand el 16 de Abr. de 2024
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 comentarios
Bruno Luong
Bruno Luong el 16 de Abr. de 2024
Editada: Bruno Luong el 16 de Abr. de 2024
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 el 16 de Abr. de 2024
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.

Iniciar sesión para comentar.

Más respuestas (1)

John D'Errico
John D'Errico el 16 de Abr. de 2024
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 comentarios
Lennart Sinjorgo
Lennart Sinjorgo el 16 de Abr. de 2024
'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 el 16 de Abr. de 2024
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

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by