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

matlab gui

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.

Best Answer

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.