How can I check existence of a variable in matlab app designer?

조회 수: 29 (최근 30일)
Sajad Mahnan
Sajad Mahnan 2017년 10월 5일
댓글: reaujahn 2022년 1월 12일
I'm creating an app designer UI. Every time I run below code in app designer, the answer is zero:
if true
% code
% Button pushed function: PreviewButton
function PreviewButtonPushed(app, event)
app.z = videoinput('winvideo',1,'RGB24_960x720');
app.x = getsnapshot(app.z);
a = exist('app.x','var');
end
On the other hand while I run this code in editor the is true:
if true
% code
z = videoinput('winvideo',1,'RGB24_960x720');
x = getsnapshot(z);
a = exist('x','var')
end
How shoud I use existence function?

답변 (2개)

Steven Lord
Steven Lord 2018년 4월 10일
The expression "app.x" is not a variable name. In this context it is an expression referring to the property x of an object stored in the variable app. [There are other potential meanings, but that's the one that's relevant here.]
If you want to check if the object stored in the variable app has a property x, use isprop.
isprop(app, 'x')
If you want to check if the property app.x exists and is non-empty:
isprop(app, 'x') && ~isempty(app.x)
If you know that the object app must have a property x (because it appears in the class definition file rather than being a dynamic property added using addprop on a class that subclasses dynamicprops) you can omit the first part of that test.
~isempty(app.x)
  댓글 수: 2
MC
MC 2018년 4월 10일
Hi Steven,
Many thanks for your prompt and full explanation. ~isempty() works perfectly for me.
This is my first time using appdesigner (only used guide previously).
Off to read up on properties now...
Best regards, Mark.
reaujahn
reaujahn 2022년 1월 12일
Hello Steven,
I have a similar question regarding ~isempty().
Would you mind having a look? I appreciate it.

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


MC
MC 2018년 4월 10일
편집: MC 2018년 4월 10일
I have the same problem.
I can evaluate the variable I am looking for while in debug mode, but 'exist' returns 0 nonetheless.
Edit: I got around it by using
try
app.x;
% continue ...
catch
% if x doesn't exist
return
end

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by