Repetitive Coding for EditText in Matlab GUI

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Friedrich
Friedrich il 17 Ott 2013
Modificato: Friedrich il 17 Ott 2013
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 Commenti
TC Ong
TC Ong il 17 Ott 2013
Thank you. It works well.
Friedrich
Friedrich il 18 Ott 2013
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su App Building in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by