Adding Strings to list box so that if two strings are same, a number will be added.
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ahmed Anas
am 4 Feb. 2020
Kommentiert: Ahmed Anas
am 5 Feb. 2020
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 Kommentare
Jakob B. Nielsen
am 4 Feb. 2020
Bearbeitet: Jakob B. Nielsen
am 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
Akzeptierte Antwort
Kevin Chng
am 5 Feb. 2020
Bearbeitet: Kevin Chng
am 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 Kommentare
Weitere Antworten (1)
Geoff Hayes
am 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).
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!