Matlab editor settings: notify undefined variables
    9 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Lennart Sinjorgo
 el 16 de Abr. de 2024
  
    
    
    
    
    Comentada: Stephen23
      
      
 el 15 de Abr. de 2025
            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
2 comentarios
  David
 el 15 de Abr. de 2025
				The asymmetry that Lennart points out here is very jarring to me as a matlab coder. Undefined variables should at least get an orange underline. It'd cut-out the first five minutes of debugging spent fixing variables with mis-spelled or mis-cased names. Sure sometimes they might be intentional. We users already have to put up with tons of orange underlines when we're partway through writing and there are tons of "unused variables" that are going to be defined in a few minutes.
  Stephen23
      
      
 el 15 de Abr. de 2025
				"Undefined variables should at least get an orange underline"
This means every function call without parentheses would also get an orange underline, as there is (in general) no way for MATLAB to use static code analysis to determinel the difference between a function call and a variable. In the OP's example y is a perfectly valid function name: how do you expect MATLAB to know the difference?
Respuesta aceptada
  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
      
      
 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
      
      
 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.
Más respuestas (1)
  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
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 comentarios
  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)
3*pi
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!