フィルターのクリア

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.

9 ビュー (過去 30 日間)
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 件のコメント
Tracy Clark
Tracy Clark 2022 年 11 月 8 日
Each name is a single cell. Here is a screenshot of what I'm getting.
Chris
Chris 2022 年 11 月 8 日
編集済み: Chris 2022 年 11 月 8 日
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.

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

採用された回答

Marcel
Marcel 2022 年 11 月 8 日
編集済み: Marcel 2022 年 11 月 14 日
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 件のコメント
Marcel
Marcel 2022 年 11 月 8 日
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

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by