How set default value in edittext in GUI

조회 수: 1 (최근 30일)
Filip M
Filip M 2016년 5월 16일
답변: MARIA RODRIGUEZ SANZ 2018년 7월 23일
Hi everyone, I have a problem with my GUI. I have to set default value for edittext (if edittext is empty ---> set variable to double 50 or string 'hello', but if not empty set variable from edittext.I do not know how can i do that. Please help me (Im working on Matlab 2013b or 2015b mac edition)

답변 (4개)

Walter Roberson
Walter Roberson 2016년 5월 17일
S = get(handles.editbox1, 'string');
if isempty(S)
S = 'hello';
end

Filip M
Filip M 2016년 5월 17일
It doesnt work. I have tried this:
function edit1_Callback(hObject, eventdata, handles)
Tstr=get(handles.edit1,'string');
Tstr=str2double(Tstr);
if isnan(Tstr)
Tstr = 58;
end
handles.T=Tstr;
It works but only if i write sth in edit1 and delete it. If i do not write sth, Matlab answer "Reference to non-existent field 'T'."
  댓글 수: 3
Filip M
Filip M 2016년 5월 17일
Seriously, i have to copy all code? i have used pushbutton.
function pushbutton1_Callback(hObject, eventdata, handles)
T=handles.T
Adam
Adam 2016년 5월 17일
편집: Adam 2016년 5월 17일
You need to show all code relevant to what you are asking. In this case the one line you just added is enough.
And did you not use
guidata( hObject, handles )
at the end of your edit1_Callback? Without this any changes you make to the handles structure in your callback are just local and are lost when the callback scope finishes. So pushbutton1_callback will see exactly the same handles structure as was passed into edit1_callback.
The above code replaces the handles structure stored in the GUI with the one you have edited so that when you trigger the next callback you get the updated 'handles' struct.

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


Filip M
Filip M 2016년 5월 17일
Ok i have added guidata to edit1 funcition but still doesnt work. With if isnan works only if I first write sth and immediately delete it. But if i start program with blank edit1 and i push button i have error
  댓글 수: 3
Filip M
Filip M 2016년 5월 17일
What do you mean?
Walter Roberson
Walter Roberson 2016년 5월 17일
I have tested and confirm that if you have not changed the String property of a uicontrol style edit, then applying str2double() to it will result in NaN, which can be caught with isnan() like you show.

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


MARIA RODRIGUEZ SANZ
MARIA RODRIGUEZ SANZ 2018년 7월 23일
I have the same problem. Did you fix it??
Thanl you very much!

카테고리

Help CenterFile Exchange에서 Word games에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by