How to underline an undefined function or variable

조회 수: 3 (최근 30일)
Heorhii Koltsov
Heorhii Koltsov 2018년 10월 5일
댓글: Heorhii Koltsov 2018년 10월 8일
Hi,
I am curious if there is a way to configure Matlab to show an undefined function or variable before running a script. For example, I want the following code to be executed if variable "a" is defined, otherwise I get notification that "a" is not set before pressing "F5".
if a>1
b=2;
end
Is there a way to configure this, maybe in Matlab code analyzer?
  댓글 수: 2
Stephen23
Stephen23 2018년 10월 5일
편집: Stephen23 2018년 10월 5일
It is easy for code to change the MATLAB Search Path while running, to run scripts and call functions which change/define/clear variables in the workspace, and to overload almost any operator. There is no way to know what code will resolve to and what variables it actually has, until it is run.
That is simple a side effect of a dynamically typed language which is parsed on the fly. It is in the very nature of such a language.
Heorhii Koltsov
Heorhii Koltsov 2018년 10월 8일
I read that there is pylint for Python which is checking the code statically and I think it will also check for the variables

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2018년 10월 5일
I believe that capability would not exist, because it is easy enough to create a variable or function name on the fly. It would be terribly poor coding practice to do so, but you CAN do it.
A quick check in the editor preferences did not show such a flag.

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 10월 5일
There is a not very practical way. You could do
if exist('a', 'var')
if a>1
b=2;
end
else
uiwait(errordlg('a does not exist'));
end
But you really don't want to do that for every variable in your program. Anyway, if you knew to check for it, then you'd know to assign it. There is no automatic way to do it before running your program. However sometimes the mlint will tell you that a variable is being used before it's assigned with a orange squiggly underline, but it doesn't always detect that.
  댓글 수: 1
Heorhii Koltsov
Heorhii Koltsov 2018년 10월 8일
Yeah that's correct. I read about exist function but of course it would be painful to write it for each variable. I guess the problem is that I am not using matlab correctly that's why I have such question :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by