필터 지우기
필터 지우기

How do I update a listbox with the strings which I enter using a inputdlg command? I tried the same, but when I enter the second string, the first string gets replaced by the new one.

조회 수: 4 (최근 30일)
I have entered the below code in the corresponding function. prompt={'Please enter the component name:'}; name='Input Component Name'; numlines=1; defaultanswer={'geometry'}; handles.answer=inputdlg(prompt,name,numlines,defaultanswer,'on'); set(handles.componentlistbox,'String',handles.answer);
My objective is, everytime when the dialogbox asks me to enter a text, the entered text should get added in the listbox in addition to the already typed text.I am using a GUIDE gui for this task. Problems I face: Each time I enter a string in the input dialog box, the previously entered string disappears. I would like to add the newly typed string below the existing string. Kindly request your support.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 7월 11일
All you need to do is append the new string to the existing list
% prompt the user for some input
answer = inputdlg(prompt,name,numlines,defaultanswer,'on');
% get the current list box selections
selections = get(handles.componentlistbox,'String');
% append the answer to the list
selections = [selections ; answer];
% update the list box with the new selection
set(handles.componentlistbox,'String',selections);
Note that in the above, the answer is not being saved to handles.answer as that duplicates what is in the list box.
Note also that you should put in a check of some kind to ensure that duplicate entries aren't added to the list.
Try the above and see what happens!
  댓글 수: 2
matlablearner
matlablearner 2014년 7월 11일
Thanks a lot for your quick response. The code solves my requirement. As you have mentioned, I will look for a way to ensure that same name entries are not added to the list again.
Have a Nice weekend. Cheers.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by