필터 지우기
필터 지우기

Error : Undefined function or variable 'I'

조회 수: 4 (최근 30일)
Hamid
Hamid 2015년 12월 2일
댓글: Stephen23 2015년 12월 2일
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
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
Hamid
Hamid 2015년 12월 2일
what should I do here??
I'm a beginner at MATLAB, please help me out.
Stephen23
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
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
Hamid
Hamid 2015년 12월 2일
I get the same error when I use the second code;
I = answer(1);
Adam
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 CenterFile Exchange에서 Scope 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