Check if a variable exists in the workspace, within a function

Hi,
I have a function which needs to check if a variable ('error_N') exists in the work space. If it does not exist, the function must create it.
In the following example, the function does not see the variable 'error_N' from the workspace. I tried to solve it by declaring it as a global variable. However, in the case the variable does not exist in the Workspace, Matlab creates an EMPTY variable called 'error_N' when reading "global error_N", so the function cannot create it with the value that I would like it to get.
Do you have any suggestions?
Thanks!
function output = function_example(input)
% global error_N ???????
E_1 = exist('error_N','var');
if E_1 ==1
else
error_N = 0.001;
end
output = function2(input);
error = output/N;
while error>error_N
blablabla
output....blablabla
end
end

4 件のコメント

Walter Roberson
Walter Roberson 2019 年 3 月 29 日
Is it intended to check the variable in the workspace of the function, or is to check the variable in the workspace of the caller of the function?
If the work is to be done in the workspace of the caller of the function, then note that MATLAB provides for "static" workspaces, and you cannot create new variables in static workspaces from inside a called function. Static workspaces exist in all cases in which there is an "end" statement matching the "function" statement. Nested functions are only permitted with static workspaces. Functions that are defined inside script files must have matching "end" statements and so have static workspaces.
Vogel
Vogel 2019 年 3 月 29 日
Hi Walter,
thanks for your reply.
It is intended to check and read the variable' 'error_N' in the workspace of the caller of the function.
If it does not exist, the function must create the variable for itsself, but does not have to go to the workspace of the caller.
Stephen23
Stephen23 2019 年 3 月 29 日
Introspective programming (such as checking for the existence of variables) is slow and leads to complex code. The MATLAB documentation recommends to avoid doing this:
Better to simply use nested functions, known variable names, and explicit logic that keeps track of what data you have.
Walter Roberson
Walter Roberson 2019 年 3 月 29 日
The path you have defined so far is:
if variable exists in caller
do nothing in current workspace
else
create variable in current workspace
end
but in the path you defined, you do not pull out the variable from the caller.

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

 採用された回答

Walter Roberson
Walter Roberson 2019 年 3 月 29 日
編集済み: Walter Roberson 2019 年 3 月 29 日

2 投票

if evalin('caller', 'exist(''error_N'',''var'')')
error_N = evalin('caller', 'error_N');
else
error_N = 0.001;
end
This would not be recommended. Better would be something like,
function output = function_example(x, error_N)
if ~exist('error_N', 'var') || isempty(error_N)
error_N = 0.001;
end
end
That is, the user passes error_N to you if the user wants something other than the default.

4 件のコメント

Vogel
Vogel 2019 年 3 月 30 日
Thank you Walter. It works :)
I usually use the second option you describe. However, it is a function that I have been using for many months in several scripts, so I would prefer not to change the inputs.
I will have to use the first option, although I have read in many pages that it is not recommended...
Stephen23
Stephen23 2019 年 3 月 31 日
編集済み: Stephen23 2019 年 3 月 31 日
"However, it is a function that I have been using for many months in several scripts, so I would prefer not to change the inputs."
Beginners often write this. At some point, as their projects get larger and more complex, they realise that their code design is inefficient and causing them to waste a lot of time (runtime, programming time, debugging time...).
Making some function rely on what exists in another workspace is inefficient and makes understanding, testing, and maintaining your code more difficult. You might not appreciate this now, but keep it in mind in the future: improving code is a good thing, not something to be avoided!
Vogel
Vogel 2019 年 4 月 1 日
Thank you for your advice :)
I will try to keep that in mind!
Hisham Abbassy
Hisham Abbassy 2019 年 12 月 12 日
I'm trying to use it for GUI and it doesn't read the variable in the loaded mat file.
I also tried to use VarLoad but it doesn't work because I get error when the variable doesn't exist in my mat file.
Do you have an idea how to solve that?

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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