How to get values from workspace to GUI with 'evalin'? error occuring
2 ビュー (過去 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 で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!