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

8 views (last 30 days)
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 Comments
Jakob B. Nielsen
Jakob B. Nielsen on 4 Feb 2020
Edited: Jakob B. Nielsen on 4 Feb 2020
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

Sign in to comment.

Accepted Answer

Kevin Chng
Kevin Chng on 5 Feb 2020
Edited: Kevin Chng on 5 Feb 2020
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 Comments
Ahmed Anas
Ahmed Anas on 5 Feb 2020
Thanks Kevin Chng, its working, am using GUIDE however, but changed the above code for GUIDE and it worked.
Ahmed Anas
Ahmed Anas on 5 Feb 2020
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

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 4 Feb 2020
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).

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!