How to get values from workspace to GUI with 'evalin'? error occuring
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Geoff Hayes
2015-9-7
编辑:Geoff Hayes
2015-9-7
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 个评论
Stephen23
2015-9-7
编辑:Stephen23
2015-9-7
@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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!