필터 지우기
필터 지우기

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

조회 수: 8 (최근 30일)
John
John 2015년 9월 7일
편집: Stephen23 2015년 9월 7일
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

채택된 답변

Geoff Hayes
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
John
John 2015년 9월 7일
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 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 CenterFile 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!

Translated by