Repetitive Coding for EditText in Matlab GUI

조회 수: 1 (최근 30일)
TC Ong
TC Ong 2013년 10월 17일
편집: per isakson 2017년 2월 10일
hi, I'm using Matlab GUI to make UI for simple calculation. I manage to do the GUI but I wish to shorten certain part in the coding.
in order to make every EditText is empty, i use the following code:
set(handles.edit1,'string','')
...
set(handles.edit20,'string','')
from edit1 until edit20.
the same coding for me to get input value from user:
a(1)=str2double(get(handles.edit1,'string'))
until a(20) with edit20
Is there anyway to make a 'for' loop to shorten this code? Thank you in advance for every advise.

채택된 답변

Friedrich
Friedrich 2013년 10월 17일
편집: Friedrich 2013년 10월 17일
Hi,
yes. For example:
>> handles.edit1 = 1;
>> handles.edit2 = 2;
>> for i=1:2
handles.(['edit',num2str(i)])
end
So in your case:
for i=1:20
set(handles.(['edit',num2str(i)]),'string','')
end
And:
for i=1:20
a(i)=str2double(get(handles.(['edit',num2str(i)]),'string'))
end
  댓글 수: 2
TC Ong
TC Ong 2013년 10월 17일
Thank you. It works well.
Friedrich
Friedrich 2013년 10월 18일
Please don't send me a PM. You can create a new Thread or leave a comment for followup questions:
"Actually I have one more question regarding the repetitive code for each edit_callback.
I got the same coding for every edittext_callback in my mFigure (that have 20 edittext) to set the edittext appear to be empty every time I run the mfile. Below is my coding:
function edit1_Callback(hObject, eventdata, handles)
if isnan(str2double(get(handles.edit1,'string')))
errordlg('You must enter a numeric value','Bad Input','modal')
uicontrol(hObject)
return
end
function edit2_Callback(hObject, eventdata, handles)
if isnan(str2double(get(handles.edit2,'string')))
errordlg('You must enter a numeric value','Bad Input','modal')
uicontrol(hObject)
return
end
same as my previous question in forum, may I know is there anyway to put the coding in a shorter way? "
You can use the same callback function for all your edit boxes. It seems like you used GUIDE. In GUIDE you can set the name of the callback function for each edit box manually. Double click on the edit box and modify the Callback entry to point to the same function, e.g. for now you have for each edit box an unique callback. Something like:
Edit1 has:
GUINAME('edit1_Callback',hObject,eventdata,guidata(hObject))
Edit2 has:
GUINAME('edit2_Callback',hObject,eventdata,guidata(hObject))
Change all of them to have the same name, e.g.
GUINAME('edit_Callback',hObject,eventdata,guidata(hObject))
And also create the edit_Callback function in the M-File belonging to your GUI.
function edit_Callback(hObject, eventdata, handles)
if isnan(str2double(get(hObject,'string')))
errordlg('You must enter a numeric value','Bad Input','modal')
uicontrol(hObject)
return

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by