Why is exist(variable,'var') not seeing a variable?

조회 수: 24 (최근 30일)
Bogdan Dzyubak
Bogdan Dzyubak 2018년 9월 24일
답변: Steven Lord 2018년 9월 24일
I have a function to which a variable is passed. The variable exists:
'signedOrNot = signed'
When I call 'exist signedOrNot var' it is found:
ans = 1
But when I use exist(signedOrNot,'var'), as I would in an if statement, it is not:
ans = 0
Any thoughts?

채택된 답변

Fangjun Jiang
Fangjun Jiang 2018년 9월 24일
exist('signedOrNot','var')

추가 답변 (1개)

Steven Lord
Steven Lord 2018년 9월 24일
This line of code tests whether the variable with the name signedOrNot exists in the workspace.
exist('signedOrNot', 'var')
This line of code tests whether the variable whose name is stored in the variable signedOrNot exists in the workspace.
exist(signedOrNot, 'var')
Compare:
% if a variable by this name exists, choose a different name
signedOrNot = 'thisVariableShouldNotExist';
passingName = exist('signedOrNot', 'var') % true, signedOrNot exists
passingContentsOfVariable = exist(signedOrNot, 'var') % false, thisVariableShouldNotExist doesn't
with:
x = 42;
signedOrNot = 'x';
passingName = exist('signedOrNot', 'var') % true, signedOrNot exists
passingContentsOfVariable = exist(signedOrNot, 'var') % true, x exists

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by