Error : Undefined function or variable 'I'
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
Réponses (2)
Stephen23
le 2 Déc 2015
Modifié(e) : Stephen23
le 2 Déc 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 commentaires
Stephen23
le 2 Déc 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.
Adam
le 2 Déc 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 commentaires
Adam
le 2 Déc 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.
Voir également
Catégories
En savoir plus sur Install Products dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!