File Exchange

image thumbnail

Dynamic Search Box

version 1.2 (45.9 KB) by

A dynamic search box key press function callback for an edit box



View License

KPF_CB was completely rewritten to work with mouse clicks and for Matlab R2011b. Currently it does not work with other releases. I haven't really looked into why yet, so I have included the original 'KPF_CB' and renamed it 'Original_KPF_CB' for compatibility.

The new KPF_CB also heavily relies on Java. The Java part was made possible by Yair Altman and his 'findjobj'.

From the help file,

% a key press callback function for an edit box
% Description
% KPF_CB() is a key press callback function for an edit box. This
% callback creates a dynamically changing listbox under or above the
% edit box (depending on space available). The contents of the listbox
% change depending on the keys pressed. This callback may be useful
% where very large lists are encountered and are difficult to sort
% through, or may act as a search box.
% Syntax
% set(edit_h,'KeyPressFcn', ...
% {@KPF_CB,jedit_h,slider_h,fig_h,list,func})
% Arguments
% edit_h - handle of the edit box to which KPF_CB is applied
% jedit_h - handle of the java component of the edit box
% slider_h - handle of the slider, if none, leave empty []
% fig_h - handle of the figure of the edit box
% list - list of selectable items
% func - function to evaluate after 'return', or 'escape' key is
% pressed or item in listbox is 'opened', if this is not needed,
% leave empty [] or do not include in edit box 'KeyPressFcn'
% callback
% Other Functionality
% - KPF_CB incorporates the functionality of many special keys
% including, backspace, delete, escape, insert, end, shift, return,
% arrow keys, and space.
% - Arrow keys allow the user to cycle left to right across the edit
% box and continue typing with a dynamically changing listbox. They
% also allow the user to scroll through the list box up and down.
% - The 'return' key will either select the closest match to the
% contents of the listbox from the contents of the edit box, or if
% the arrow keys were previously used to cycle through the listbox,
% then whichever item is currently highlighted will be selected. This
% selected word will then appear in the edit box, and the listbox will
% be deleted.
% - Other keys behave as expected
% - If an item in the listbox is clicked, the edit box will display
% the item clicked on, but the contents of the listbox will remain the
% same. If an item in the listbox is opened (i.e. double-clicked),
% this will have the same effect as the 'return' key, but for the item
% clicked on.
% - A slider can be used with this function. For example, if multiple
% edit boxes were placed on a panel with a slider, the listbox would
% change size depending on the position of the edit box and slider.
% - The optional function to evaluate after 'return', or 'escape' key
% is pressed or item in listbox is 'opened' allows for immediate
% function evaluation without needing a 'push button'
% Example
% list = cell(26,26,26);
% for i1 = 1:26
% for i2 = 1:26
% for i3 = 1:26
% list{i1,i2,i3} = [char(64+i1) char(64+i2) char(64+i3)];
% end
% end
% end
% list = reshape(list,numel(list),1);
% scrnsz = get(0,'ScreenSize');
% fig_h = figure('MenuBar','none','Resize','off', ...
% 'Position',[scrnsz(3)/2-100 scrnsz(4)/2-100 200 200]);
% edit_h = uicontrol('Style','edit','Position',[6 170 190 25]);
% jedit = java(findjobj(edit_h));
% jedit_h = handle(jedit,'CallbackProperties');
% set(edit_h,'KeyPressFcn',{@KPF_CB,jedit_h,[],fig_h,list,@my_fun})
% set(jedit_h,'MouseReleasedCallback',{@KPF_MR_CB,edit_h}, ...
% 'MousePressedCallback',{@KPF_MP_CB,edit_h})
% function my_fun
% get(edit_h,'String')
% end
% Notes
% - In order to close the listbox an item must be selected, by either
% pressing either the 'escape' or 'return' keys or 'opening' an item in
% the listbox
% - This function makes use of the edit box user data variable
% - Java is used extensively
% - This function is not bug free yet, but usually hitting 'backspace'
% or 'delete' enough times will clear whatever my function thinks is in
% there

% Version : 1.1 (10/06/2011)
% Author : Nate Jensen
% Created : 09/21/2011
% History :
% - v1.0 (09/21/2011) : initial release
% - v1.1 (10/06/2011) : added java component to enable mouse clicks

Comments and Ratings (6)

Nate Jensen

Nate Jensen (view profile)

Thank you Saurabh!

Nate Jensen

Nate Jensen (view profile)

Thanks Vincent!

At the moment I am not sure, but I will look into it. No guarantees on a time frame though.


Hi there, really great work! I modified your function in order to get it work with previous versions and I've got a question:

Is it possible to pass the output of the function-handle to a output-variable (so I'd like to call the function like
fl = filterList(list);
and want a filtered list in fl...

Thank you!

Nate Jensen

Nate Jensen (view profile)


Try changing the first line from,
function KPF_CB(~,event,edit_h,parent_h,slider_h,fig_h,list,func)
function KPF_CB(obj,event,edit_h,parent_h,slider_h,fig_h,list,func)

I am not familiar with all of the changes that Matlab made between different releases, so that may not be the only issue. The full documentation can be viewed here:

If you run into other issues, let me know.

Good luck,

Andre Asfar

I have this error:

??? Error: File: KPF_CB.m Line: 1 Column: 17
Unexpected MATLAB operator.

refers to this operator: ~

I´m using the version 7.6.0 (R2008a), is there any posibility to make run this file in my version?




completely rewrote code, added more comments, added java component to capture mouse clicks, does not work properly in other versions yet, renamed submission


v1.01 hot fix

MATLAB Release
MATLAB 7.13 (R2011b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video