UI without GUIDE. The problem with edit element

조회 수: 1 (최근 30일)
Sergey Lopatnikov
Sergey Lopatnikov 2016년 4월 26일
편집: Stephen23 2016년 4월 26일
I try make UI without GUIDE
I wrote simple code:
function testui()
f=figure();
f.Color='black';
f.ToolBar='none';
ui=uicontrol('Style','edit');
ui.HorizontalAlignment='right';
ui.String='0';
myvar=2;
pb=uicontrol('Style','pushbutton','Position',[250 20 60 20],'String', 'Evaluate', 'Callback',{@pushbutton_callback,myvar,ui.String})
end
function pushbutton_callback(hObject,callbackdata,x,Y)
F=str2double(Y);
display(F)
display (x)
end
The problem is that even if I enter in edit box some string (number< say 10 and click "Enter", which must change the handle ui.String), I do not get this number in pushbutton_callback function. I get only predefined ui.String (if not empty): F = 0 x = 2 Instead of F = 10 x = 2 as expected. What do I wrong?
[EDITED, Jan. Please format your code - Thanks!]

채택된 답변

Robert
Robert 2016년 4월 26일
You should be passing into the callback function the handle to your edit box. For example
... ,'Callback',{@pushbutton_callback,myvar,ui})
Then get the value from the edit box inside the callback. The way you are doing it, MATLAB is evaluating the value of the string and saving the literal as part of the callback function call. It will not evaluate it again.
With the revisions, your code becomes:
function testui()
f=figure();
f.Color='black';
f.ToolBar='none';
ui=uicontrol('Style','edit');
set(ui,'HorizontalAlignment','right') % set works in R2014a and older, too
set(ui,'String','0')
myvar=2;
uicontrol('Style','pushbutton','Position',[250 20 60 20],'String', ...
'Evaluate', 'Callback',{@pushbutton_callback,myvar,ui});
end
function pushbutton_callback(~,~,x,Y) % use ~ to ignore inputs we don't use
F=str2double(get(Y,'String'));
display(F)
display(x)
end
  댓글 수: 2
Sergey Lopatnikov
Sergey Lopatnikov 2016년 4월 26일
thanks. It works.
Robert
Robert 2016년 4월 26일
If this answered your question, please consider accepting the answer using the blue "Accept this Answer" button above.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by