Adding Strings to list box so that if two strings are same, a number will be added.

조회 수: 8 (최근 30일)
I have a listbox and add button and an editable text.
On clicking Add button, text will be taken from the edit box and will be added to the listbox.
What i want is that if 1st string of listbox is Material, and the user again entered 'Material' for addition, it will be changed to Material1 and then added to listbox and so on.
Thanks all.
  댓글 수: 2
Jakob B. Nielsen
Jakob B. Nielsen 2020년 2월 4일
편집: Jakob B. Nielsen 2020년 2월 4일
Use the stringcompare function (perhaps the case insensitive one). If the entered string is named newstring and your listbox strings are contained in a cell named listboxcontent, for example;
for i=1:length(listboxcontent)
tf(i)=strcmpi(listboxcontent{i},newstring)
end
if sum(tf)>0 %that means at least 1 instance (and hopefully only one) of newstring existed.
listboxcontent{length(listboxcontent)+1}=[newstring,'1']; %Add a new string to the next index,
%then insert the code you use to update your listbox and so on here.
end

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

채택된 답변

Kevin Chng
Kevin Chng 2020년 2월 5일
편집: Kevin Chng 2020년 2월 5일
Hi,
(Accept my answer if you found my solution is working for your case, thanks)
Here you go:
Step 1 : Create the said GUI
Step 2 : Create Callback function for Button
Step 3 : Script for the Callback function
or you can copy from here
new=app.EditField.Value;
itemlist = app.ListBox.Items;
match_number=sum(strncmp(new,itemlist,length(new)));
if match_number == 0
itemlist{end+1} = new;
app.ListBox.Items = itemlist;
else
new_name = sprintf("%s%i",new,match_number);
itemlist{end+1} = char(new_name);
app.ListBox.Items = itemlist;
end
It is working perfectly. If you have any doubt on the algo, let me know.
  댓글 수: 2
Ahmed Anas
Ahmed Anas 2020년 2월 5일
Thanks Kevin Chng, its working, am using GUIDE however, but changed the above code for GUIDE and it worked.
Ahmed Anas
Ahmed Anas 2020년 2월 5일
For GUIDE, following can be used..
new=get(handles.edit1,'string') %% New String to be added
itemlist=get(handles.listbox1,'string') %% Previous List
match_number=sum(strncmp(new,itemlist,length(new)));
if match_number == 0
itemlist{end+1} = new;
set(handles.listbox1,'string',itemlist)
else
new_name = sprintf('%s%i',new,match_number);
itemlist{end+1} = char(new_name);
set(handles.listbox1,'string',itemlist)
end

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

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 2월 4일
Ahmed - I think that you may want to manage, outside of the list box, a separate list of all the strings that have been added to the list box and a count of each time that string has been added. This could be a cell array with two columns: the first being the strings, and the second being a count of each string. When the user presses the Add button, the code would compare against each string in the first column of your cell array. If a match is found, then the second column is incremented by one. If no match is found, then the string is added to the cell array (in the first column) with a zero (or one) count (in the second column). You would then add this string to the list box, adding the numeric suffix if needed.
I suppose an alternative to a cell array is a map with the key being the input string.
Note that how you store and use this cell array (or whatever container you decided to use) depends upon how you have coded your GUI (with GUIDE, programmtically, or with App Designer).

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by