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

7 次查看(过去 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
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