MATLAB: How to check whether the variable is present or not

variable

hi, I have tried if condition with the help of "exist" as:
checkVariable1= exist('lbl_edge','var');
if checkVariable1==1
edge= newedge1;
else
edge = newedge2;
end
I have also tried
if any(strcmp(who,'lbl_edge'))==1
....
else
...
end
But none of them are working. Can you please help me…
Thanks

Best Answer

Works on mine. Try this code.
clear;clc;close all
checkVariable1= exist('lbl_edge','var');
if checkVariable1==1
disp('Exist')
else
disp('didn''t exist')
end
disp('now creating the variable.')
lbl_edge='something';
checkVariable1= exist('lbl_edge','var');
if checkVariable1==1
disp('Exist')
else
disp('didn''t exist')
end
Gives this output
didn't exist
now creating the variable.
Exist