Error : Undefined function or variable 'I'

3 views (last 30 days)
Hamid
Hamid on 2 Dec 2015
Commented: Stephen23 on 2 Dec 2015
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.

Answers (2)

Stephen23
Stephen23 on 2 Dec 2015
Edited: Stephen23 on 2 Dec 2015
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 Comments
Hamid
Hamid on 2 Dec 2015
what should I do here??
I'm a beginner at MATLAB, please help me out.
Stephen23
Stephen23 on 2 Dec 2015
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.

Sign in to comment.


Adam
Adam on 2 Dec 2015
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 Comments
Hamid
Hamid on 2 Dec 2015
I get the same error when I use the second code;
I = answer(1);
Adam
Adam on 2 Dec 2015
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.

Sign in to comment.

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!