フィルターのクリア

How to create a search box in List Box in MATLAB GUI?

37 ビュー (過去 30 日間)
Harsh Mittal
Harsh Mittal 2021 年 8 月 13 日
回答済み: Image Analyst 2021 年 8 月 13 日
I have a listbox in my MATLAB GUI, it contains 200 items to select from. I want to add a search option in my listbox so that I can select the items from the list box easily, is there any way to do this?
  2 件のコメント
Adam Danz
Adam Danz 2021 年 8 月 13 日
You can add a textbox to your GUI. The callback of the textbox will update the listbox list by deciding which values are a match to the string in the text box. You may want to use startsWith, endsWith, regexp, regexpi, contains, or another string matching function.
Harsh Mittal
Harsh Mittal 2021 年 8 月 13 日
Thanks I will try this to resolve my issue.

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

採用された回答

Image Analyst
Image Analyst 2021 年 8 月 13 日
I agree with Adam. I'd put an edit text box right above the listbox. Maybe with a button beside it if you want. Then in the callback for the edit text box, and the button callback if you have a button, call a function called LoadListBox(). In that function do something like (untested)
function handles = LoadListBox(handles)
% Get the folder you're working in.
folder = handles.folder; % or wherever....
% Get edit text box pattern
filePattern = strtrim(handles.edtSearch.String);
% Prepend the folder.
if isempty(filePattern)
% Handle case where the edit box is empty.
filePattern = fullfile(folder, '*.*'); % List all files.
else
% Case where they entered something into the edit text box.
% See if they put an extension. If they didn't, add star.
if ~contains(filePattern, '.') || endsWith(filePattern, '.')
% No dot, or else ends with dot but has no extension. Add wildcard extension.
filePattern = [filePattern, '.*']; % Append dot star.
end
filePattern = fullfile(folder, filePattern);
end
% Call dir to get a list of files matching the filter.
fileList = dir(filePattern);
% Send the list of base file names into the listbox control.
handles.lstFilebox.String = {fileList.name};
% Old Value (from prior folder listing) cannot be greater than the number of files in this folder or
% else the listbox won't appear. It it's longer, make it 1.
if handles.lstFilebox.Value > length(handles.lstFilebox.String)
handles.lstFilebox.Value = 1;
end

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by