Error using evalin Undefined function or variable 'var'.

50 ビュー (過去 30 日間)
Samuel Jesus
Samuel Jesus 2023 年 3 月 9 日
コメント済み: Jan 2023 年 3 月 10 日
Hello all!
I'm trying to figure out why I keep getting the error: Undefined function or variable 'var'.
Here's my code:
function Port_callback(varargin)
GUI=varargin{numel(varargin)};
var=string(get(GUI.ed(1),'String'));
fprintf('Inserted USB Port: %s\n',var);
evalin('base',"Port=var;");
end
The string in var is printed to the command prompt using fprintf, however in the next line of code i keep getting the error undefined variable 'var'.
Any ideas on why this error keeps occurring?

採用された回答

Jan
Jan 2023 年 3 月 9 日
編集済み: Jan 2023 年 3 月 9 日
The variable var is created in the workspace of the callback function. It is not visible in other workspaces, e.g. in the command window (the "base" workspace).
This would work:
assignin('base', 'Port', var)
Note that poking variables magically in other workspaces is a frequent souce of bugs and unexpected behavior. If someone else is using your GUI, it is hard to understand, where the value of Port is coming from. Everything, which impedes the debugging and maintenance, is the enimy of the programmer.
A clean solution is to store the value of the Port inside the GUI (see handles struct or UserData) and trigger the further processing from another callback. The indirection over the command window increases the complexity.
  3 件のコメント
Steven Lord
Steven Lord 2023 年 3 月 9 日
In this particular case, that assignin call could cause you quite a bit of confusion later on in your MATLAB session. In the example below your assignin call would take the place of the explicit assignment to the variable var.
var = 42;
Now suppose you wanted to compute the variance of a data set. What's the right way to do that?
data = 1:10;
var(data) % You might expect 9.1667, but you get an error
Index exceeds the number of array elements. Index must not exceed 1.

'var' appears to be both a function and a variable. If this is unintentional, use 'clear var' to remove the variable 'var' from the workspace.
Ah, we've improved the error message in some recent release. In older releases the second line of the error message didn't appear, leaving you to wonder why the var function misbehaved.
Jan
Jan 2023 年 3 月 10 日
@Steven Lord: The assigned variable is "Port", not "var". The latter ist used internally in the callback only.

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

その他の回答 (1 件)

Voss
Voss 2023 年 3 月 9 日
This line
evalin('base',"Port=var;");
evaluates "Port=var;" in the base workspace. Apparently you don't have a variable called "var" in the base workspace.
If you want to have a variable "Port" in your base workspace with the value of "var" that is in the Port_callback workspace, you can
assignin('base','Port',var);
  1 件のコメント
Samuel Jesus
Samuel Jesus 2023 年 3 月 9 日
Thanks Voss! I saw Jan response first, but yours is equally right!

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

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by