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

44 ビュー (過去 30 日間)
Tony
Tony 2011 年 4 月 22 日
コメント済み: Gaganjyoti Baishya 2020 年 6 月 20 日
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.
  2 件のコメント
Joel Ngouadjeu
Joel Ngouadjeu 2016 年 12 月 28 日
編集済み: Joel Ngouadjeu 2016 年 12 月 28 日
ismember('var',evalin('base','who'))
this also work very fine where var is your variable.
Steven Lord
Steven Lord 2016 年 12 月 29 日
While it is possible to do this, it's not a good idea to do this. You're giving any function that has access to the base workspace (i.e. any function that can call assignin) the ability to control what your function does.
Imagine if you gave anyone with a web browser the ability to control the temperature setting of the burners on your stove. You'd come home one day to find the fire department outside your home (or perhaps the pile of ash and soot that used to be your home.)

サインインしてコメントする。

採用された回答

Sean de Wolski
Sean de Wolski 2011 年 4 月 22 日
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.
  1 件のコメント
Jan Berling
Jan Berling 2018 年 1 月 29 日
This answer is not fully correct, because it checks if there is any 'A' in all variable names of the caller workspace. The square bracket creates an array of chars and should be replaced by curly brackets which create a cell array of strings.
W = evalin('caller','whos'); %or 'base'
doesExist = ismember('A',{W(:).name})
Otherwise stay with the Comment from Joel Ngouadjeu on 28 Dec 2016.

サインインしてコメントする。

その他の回答 (2 件)

Paulo Silva
Paulo Silva 2011 年 4 月 22 日
In a script you don't need the evalin because the variables are in the same scope as the workspace ones, in a function do this:
try
A=evalin('base','A');
catch
A=[];
end
The code also works in the workspace or a script but it's best just to
if ~exist('A','var'),A=[];end

Tony
Tony 2011 年 4 月 22 日
function InBase=fnBaseExist(var1)
W = evalin('base','whos');
InBase=0;
for ii= 1:length(W)
nm1=W(ii).name;
InBase=strcmp(nm1,var1)+InBase;
%InBase = ismember(var1,W(ii).name)+InBase
end
InBase(InBase>0)=1;
This stops an issue where the variable name was part of an existing variable.
  2 件のコメント
Jan Berling
Jan Berling 2014 年 2 月 17 日
function InBase=fnBaseExist(var1)
W = evalin('base','who');
InBase=0;
for ii= 1:length(W)
nm1=W{ii};
InBase=strcmp(nm1,var1)+InBase;
end
InBase(InBase>0)=1;
This modification of Tonys' function reduces the calculation time, especially if there is a lot of data in the workspace. The "who" command only lists the names of the variables, whereas the "whos" command also lists the size of the variables, etc.. (With Matlab under memory stress, whos took about 1.000s and who nearly 0s.)
Gaganjyoti Baishya
Gaganjyoti Baishya 2020 年 6 月 20 日
try
x=evalin('base', 'yourVar');
catch
x=0
if ~x x is not present in base
else x is present

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by