UIControl callback function problems

3 views (last 30 days)
Jon
Jon on 31 Mar 2014
Commented: Jon on 7 Apr 2014
Ok, I'm trying out UIControl for the first time and I just want to write a program that asks a user to type something in after pressing a button and then displaying the string.
This is the script:
h = figure(1);
str = [];
h = uicontrol('Style', 'Pushbutton', 'String', 'A', 'Position', [255, 185, 50, 50], 'Callback', @push);
h = uicontrol('Style', 'Text', 'String', str, 'Position', [235, 250 ,100, 25]);
And the function:
function push(hObject, eventdata)
str = input('Enter value: ', 's');
set(h, 'String', str)
drawnow
end
It seems that the function doesn't recognise the variable 'h'. "Undefined function or variable 'h'.
"Error in push (line 3) set(h, 'String', str)
Error while evaluating uicontrol Callback" I know it's probably something trivial, but like I said, it's my first time in this area. Thanks for any help.

Accepted Answer

Dishant Arora
Dishant Arora on 31 Mar 2014
h = figure(1);
str = [];
handles.push1 = uicontrol('Style', 'Pushbutton', 'String', 'A',...
'Position', [255, 185, 50, 50])
handles.text1 = uicontrol('Style', 'Text', 'String', str,...
'Position', [235, 250 ,100, 25]);
set(handles.push1,'Callback', {@push,handles});
function push(hObject, eventdata,handles)
str = input('Enter value: ', 's');
set(handles.text1, 'String', str)
drawnow
end
  1 Comment
Jon
Jon on 7 Apr 2014
Not exactly what I wanted. I just found that handle, assigned it to a variable and used that. But thanks for your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!