How to set the default value for Edit Text in GUI ? If not input a number , I hope the contents auto changed to be '5'

조회 수: 1 (최근 30일)
In the function edit1_callback(),it is writed as :
edit1 = str2double(get(hObject,'string'));
if isempty(edit1)
edit1 = 5; % when not input a number ,the contents is 5;
end
handles.edit1 = edit1;
guidata(gcbo,handles);
As a matter of fact,when I do not input a number,the edit text contents is not 5,Why?
And ,how can I set the default value of edit text??
Thank you for your attention!

채택된 답변

Teja Muppirala
Teja Muppirala 2012년 11월 19일
STR2DOUBLE returns NaN for non-numeric data. Instead of checking for empty input using ISEMPTY, use ISNAN instead.
if isnan(edit1)
set(handles.edit1,'string','5');
end
  댓글 수: 2
Lisa Wu
Lisa Wu 2012년 11월 19일
편집: Lisa Wu 2012년 11월 19일
As you said: function edit1_callback() edit1 = str2double(get(hObject,'string')); if isnan(edit1) set(handles.edit1,'string','5'); end guidata(gcbo,handles);
funciton pushbutton1_callback() x = handles.edit1; y = x+7; plot(x,y,'*r'); guidata(gcbo,handles);
When run it ,the default value of x is not 5 ,i'm puzzled. Thank you !
Arthur
Arthur 2012년 11월 19일
You forgot to store edit1. And, I think it's better to retrieve the value of handles.edit1 in the pushbutton callback. Try this:
function edit1_callback()
edit1 = str2double(get(hObject,'string'));
if isnan(edit1)
set(handles.edit1,'string','5');
end
function pushbutton1_callback()
x = str2double(get(handles.edit1,'String'));
y = x+7;
plot(x,y,'*r');

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by