Error : Undefined function or variable 'I'
3 次查看(过去 30 天)
显示 更早的评论
Hi everyone, I'm using GUI tool.
I will get the error after running the file.
...
prompt = {'rms symmetrical line to ground fault current in kA:'}
title = 'Ground Grid Inputs';
lines = 0.8;
def = {''};
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
answer=str2double(inputdlg(prompt,title,lines,def,options));
assignin('base','I',answer(1));
...
A=I;
...
what should I do?
thanks fellas.
回答(2 个)
Stephen23
2015-12-2
编辑:Stephen23
2015-12-2
Stop using assignin for trivial passing of variables. When you pass variables properly and don't just "poof" them into existence you will not get error messages like this.
The best practice is to pass arguments, and the worst practice is to "poof" arguments into existence in a workspace:
And here explanations of why dynamically creating variables is bad code:
2 个评论
Stephen23
2015-12-2
Two people have told you to avoid using assignin, and I also gave you links to the documentation showing how to properly pass variable between workspaces and callbacks. Read them and practice some of the examples.
Adam
2015-12-2
I never use
assignin( 'base',... );
as I've never found any reason why I would want to do such a thing, but from what I understand of it that will assign 'I' in the base workspace, but then lower down your file you try to access it in the workspace you are currently working in, but it isn't defined there.
You should just be able to remove the assignin instruction and replace it with:
I = answer(1);
This is how code flow should ideally work - you define variables in a workspace and use them in that same workspace or return them from functions to whatever workspace called them. Parachuting them into the base workspace is not something I could see as being good practice.
or if you really need it in the base workspace also then put this line in additionally.
2 个评论
Adam
2015-12-2
Then that probably depends on what is in the ... code that you missed out. If you define a variable, I, as above then it will exist throughout that same workspace (i.e. within the function if you are in a function, but not after the function ends) unless you explicitly clear the variable with instructions such as
clear all
clearvars
or less likely, but more pointedly
clear I
Based on the code you have shown I see no reason why 'I' should not still exist though if you define it.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Install Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!