Hi, I am trying to create a dropdown list search in AppDesigner, that look for a string within the word, not just at the start.

3 views (last 30 days)
My list is hundred of chemicals long and I want to search a dropdown list which will show me all these options containing the word formaldehyde, not just those starting with the word.
I tried this (an answer for a similar question) and it restricts the list as required, but I still only get the options starting with 'formald' come up in my dropdown.
app.NameLookupDropDown.Value = 'formald'
if isempty(app.NameLookupDropDown.Value) % If empty
app.NameLookupDropDown.Items = app.ChemList; % whole dataset
else
idx = contains(app.NameLookupDropDown.Items, app.NameLookupDropDown.Value);
Filtered_Values = app.NameLookupDropDown.Items(idx);
app.NameLookupDropDown.Items = Filtered_Values;
end
----
Formaldehyde
Formaldehyde polymer with 4-(1,1-dimethylethyl)phenol
Formaldehyde mixt. with methylphenol
Sodium formaldehydesulfoxylate
Naphthalenesulfonic acid, polymer with formaldehyde, sodium salt
4-tert-Butylphenol formaldehyde resin
Paraformaldehyde
N-[4-[[4-(dimethylamino)phenyl]phenylmethylene]-2,5-cyclohexadien-1-ylidene]-N-methylMethanaminium chloride, mixt. with formaldehyde
Pentanedial, mixt. with alkylbenzyldimethylammonium chlorides, ethanedial and formaldehyde
  3 Comments
Chris
Chris on 8 Nov 2022
Edited: Chris on 8 Nov 2022
It's difficult to diagnose without the full code, but a simple fix might be adding 'IgnoreCase',true to the contains call.
idx = contains(app.NameLookupDropDown.Items, app.NameLookupDropDown.Value, 'IgnoreCase',true);
On the other hand, since your code isn't working as it seems like it should, you might try Marcel's example, or troubleshoot.
You can put a breakpoint at the start of the if block, and when the program pauses, switch to the Matlab IDE window to inspect the app/variable properties. Determine whether they match what you expect to see, and you can inject code via the command window, to make sure the command you want to use is working properly.

Sign in to comment.

Accepted Answer

Marcel
Marcel on 8 Nov 2022
Edited: Marcel on 14 Nov 2022
EDIT
I changed the code as it didnt work 100%. Now it should.
Okay so i might have managed to make a solution. Im pretty sure you can adopt this to your application as well.
So in the startup function im declaring the data:
% Code that executes after component creation
function startupFcn(app)
global data;
data = [
"Formaldehyde",
"Formaldehyde polymer with 4-(1,1-dimethylethyl)phenol",
"Formaldehyde mixt. with methylphenol",
"Sodium formaldehydesulfoxylate",
"Naphthalenesulfonic acid, polymer with formaldehyde, sodium salt",
"4-tert-Butylphenol formaldehyde resin",
"Paraformaldehyde",
];
end
I added a few basic elements for testing using the App Designer and wrote the following code.
% Value changed function: ClearButton
function ClearButtonValueChanged(app, event)
value = app.ClearButton.Value;
global data;
clearitems(app);
for i=1:length(data)
app.ListBox.Items = [data(i), app.ListBox.Items];
end
end
% Value changed function: SearchButton
function SearchButtonValueChanged(app, event)
value = app.EditField.Value;
clearitems(app);
global data;
searchLength = strlength(value);
for i=1:length(data)
data(i);
for a=1:strlength(data(i))
if contains(lower(data(i)), lower(value)) == 1
% Get the items form the listbox
str = get(app.ListBox, 'Items');
str = cellstr(str);
if find(ismember(str, (data(i))))
% A Item with the value already exist
else
% There was no item with the value in the listbox,
% therefore we can add it
app.ListBox.Items = [data(i), app.ListBox.Items];
end
end
end
end
end
function clearitems(app)
app.ListBox.ItemsData = [];
app.ListBox.Items=cellstr(num2str([]));
end
Results:
  4 Comments
Marcel
Marcel on 8 Nov 2022
yeah i see what you mean. im more of a "proof of concept" guy and when something works then im trying to make it better, but in this case in my opinion its not that bad and well should only show a way (of many) on how you could achieve this.
But thats the cool thing about communities and colaboration as we can help each other and improve :P

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!