How to display data from editext in listbox in GUI ?
2 views (last 30 days)
Show older comments
Hi everyone!
My problem is:
I have a GUI contains a listbox and a edittext.
Listbox have a array of strings:
a
an
able
back
bath
bench
car
create
common
claim
dad
did
dear
eat
edit
english
enable
far
father
fish
jacket
jet
kiss
gather
get
gun
...
and so on
Now, i want to import a word "jacket" (for example) on edittext. i expect when i import word "j" on edittext, the listbox will display all word that begin with word "j". Similarly, when i import consecutive word, "a", (now, i'm having "ja" on edittext). the listbox will display all word that contains words "ja". and so on
Thank you so much
6 Comments
Accepted Answer
Kevin Phung
on 16 Jan 2019
the contains() function can help you out here, or if you want to get fancy, regexp()
Set your callback for the 'edit' box to retrieve the strings from the list box,
and do
contains(list_box_string,edit_box_string)
This will give you a logical array, simply use that array as indexing for the strings in the listbox.
Note: You may want to save the main list under a variable to be called when edit box is empty, so that you preserve it and it doesnt get lost when you change the string of the listbox.
Let me know if this helps or if you need more clarification.
13 Comments
Kevin Phung
on 21 Jan 2019
Nope, Chinese. But my parents are from there so vietnamese culture is heavily a part of mine!
More Answers (1)
Jan
on 18 Jan 2019
Edited: Jan
on 22 Jan 2019
UNTESTED!!!
function yourGUI
StrList = {'a', 'an', 'able', 'back', 'car', 'create'};
H.Fig = figure('Position', [100, 100, 400, 600], ...
'KeyPressFcn', @myKeyPress);
H.Input = uicontrol(H.Fig, 'Style', 'edit', 'Position', [10, 550, 200, 30], ...
'Enable', 'inactive', 'String', '');
H.List = uicontrol(H.Fig, 'Style', 'listbox', 'Position', [10, 10, 200, 530], ...
'String', StrList, ...
'Min', 0, 'Max', 2); % Max-Min > 1: Multiple selections allowed
guidata(H.Fig, H);
end
function myKeyPress(FigH, EventData)
H = guidata(FigH);
Str = get(H.Input, 'String');
StrList = get(H.List, 'String');
Match = find(strncmpi(Str, StrList, length(Str)));
switch edata.Key
case 'return'
% If only 1 string in the string list is matching,
% RETURN copies it to the edit field:
if numel(Match) == 1
Str = StrList{Match}; % [EDITED]
end
case 'backspace'
if ~isempty(Str)
Str(end) = [];
end
case 'delete'
... ???
% case Arrow keys?!
otherwise
c = EventData.Character;
if ~isempty(c)
Str = [Str, c];
end
end
set(H.Input, 'String', Str);
Match = find(strncmpi(Str, StrList, length(Str)));
set(H.List, 'Value', Match); % Select all matching strings in the list
if isempty(Match) % Move fiorst selected string to top
set(H.List, 'ListBoxTop', 1);
else
set(H.List, 'ListBoxTop', Match(1));
end
end
This function was written inthe forum's editor and not checked. It is thought only to demonstrate, how the KeyPressFcn of the figure can be used to emulate an edit field with running a callback after each keystroke.
5 Comments
Jan
on 22 Jan 2019
Edited: Jan
on 22 Jan 2019
My code should behave almost the same. If you type "ju", all strings in the list starting with "ju" are selected. When only one selection is matching, pressing ENTER wil copy it.
I cannot post exactly matching code, because I do not see the original code and the GUI. But I assume, the shown methods should be sufficient to allow you to apply the required modifications.
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!