MATLAB: Have a function check whether a variable already exists in the base workspace

baseMATLABvariable

if ~exist('A','var')
A=[];
else
A=evalin('base','A');
end
This code works great from a script, but not from within the function since it has its own workspace. I do not see an option to check in the base workspace.

Best Answer

W = evalin('caller','whos'); %or 'base'
doesAexist = ismember('A',[W(:).name])
This won't check if what it is, just that it exists, but you could figure that out by looking at the other fields of W.