Effacer les filtres
Effacer les filtres

How to get values from workspace to GUI with 'evalin'? error occuring

8 vues (au cours des 30 derniers jours)
John
John le 7 Sep 2015
Modifié(e) : Stephen23 le 7 Sep 2015
Hi,
i am just about creating my first complex GUI in MATLAB and i have one question plz. I created a GUI_1 that with the help of a push button starts a GUI_2. In GUI_2 the user can give some input values.
those input values (here for example the variable 'a') are given to the workspace with:
assignin('base', 'var1', a);
This works fine!
>> who
Your variables are:
var1 var2
Now i want to have access to this variable that i named 'var1'. This access should be in a Callback from a push Button in GUI_1. Pressing this Push Button shall load the variables from workspace and start the main script.
I use the function 'evalin' for this:
function run__Callback(hObject, eventdata, handles)
% hObject handle to run_ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
temp1=evalin('base',var1)
Pushing the Push Button results in that error:
Undefined function or variable 'var1'.
Error in Try1>run__Callback (line 259)
temp1=eval('base',var1)
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Try1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Try1('run__Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
How can it be? i checked the variables that are in the workspace and i saw that
function run__Callback(hObject, eventdata, handles)
% hObject handle to run_ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
who
%temp1=evalin('base',var1)
results in
Your variables are:
eventdata hObject handles
so why arent there var1 and var2 in the workspace anymore??? why are they lost...?
Best regards, John

Réponse acceptée

Geoff Hayes
Geoff Hayes le 7 Sep 2015
Modifié(e) : Geoff Hayes le 7 Sep 2015
John - in your line of code
temp1=evalin('base',var1)
the var1 should be wrapped in single quotes since the second input is an expression (see evalin for details). Try instead
temp1=evalin('base','var1')
Note that there is an alternative to using (polluting) the base workspace to share data between GUIs. See http://www.mathworks.com/matlabcentral/answers/146215-pass-data-between-gui-s which describes a couple of ways on how you can accomplish this.
  2 commentaires
John
John le 7 Sep 2015
Thank you very much Geoff!! it works fine now! nevertheless i may take a look at the other opportunity and follow your link!
Thanks!!!!!:)
Stephen23
Stephen23 le 7 Sep 2015
Modifié(e) : Stephen23 le 7 Sep 2015
@John: actually evaluating variables into existence is the worst way of doing this. If you want to learn how to write faster, more robust and better code then it is worth learning how to pass variables properly between callbacks and workspaces.
Although using eval and evalin are very popular with beginners, there is a reason why MATLAB advises to avoid them: they are slow and buggy. Learn better ways to program.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT-Files 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!

Translated by