Why eval in GUI does not execute?

1 view (last 30 days)
Daniel N
Daniel N on 2 May 2015
Commented: Daniel N on 3 May 2015
I am new to GUI design and coding. I have the following in my code but when I fill in my edit text box with a command
im2bw(im);
it does not run when I hit the button. I double-checked the pushbutton tag and it is correct.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im
s = get(handles.edit1,'string');
eval(s)
axes(handles.axes1);
imshow(im);

Accepted Answer

Adam
Adam on 2 May 2015
So what exactly does happen?
Matlab variables (except for objects of a handle-derived class in OOP) are not passed to functions by reference, rather by value. So just calling
im2bw(im);
would do the calculation then throw the result away. Then you simply plot the image afterwards.
So assuming there were no errors and all you saw was your final image that would be the reason.
I never use either global variables (I assume im does actually exist at that point) or eval, but I imagine if you enter:
im = im2bw(im);
into the edit box it may work. (Or manipulate the string from the edit box to add that in in the function rather than the user having to do it.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!