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
Ahmed Anas
Ahmed Anas 2020 年 2 月 4 日
It didnt work, brother...

サインインしてコメントする。

採用された回答

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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by