How to display data from editext in listbox in GUI ?

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Jan
Jan il 17 Gen 2019
I do not understand, what "the keypress to be the enter key" means.
Le Dung
Le Dung il 18 Gen 2019
Dear Jan,
Could you explain more detail about how to use WindowsKeyPressFcn for my problem?

Accedi per commentare.

Risposta accettata

Kevin Phung
Kevin Phung il 16 Gen 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 Commenti
Kevin Phung
Kevin Phung il 21 Gen 2019
Nope, Chinese. But my parents are from there so vietnamese culture is heavily a part of mine!

Accedi per commentare.

Più risposte (1)

Jan
Jan il 18 Gen 2019
Modificato: Jan il 22 Gen 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 Commenti
Le Dung
Le Dung il 22 Gen 2019
Modificato: Le Dung il 22 Gen 2019
Dear Jan, matlab hasn't returned the error. But, my GUI didn't work as i expect.
Perhap, i described wrong that i expect.
Actually, i only want as:
when i type "j" on editbox, my GUI (or Matlab) take "myselection" to first word of word group that presents all word that start with "j" (jacket, jab, jabber, jar, juba, jube, jubile....), and for consecutive word, for example, "u", (we have "ju"). Again, my GUI (or Matlab) take "myselection" to first word of word group that presents all word that start with "ju" (juba, jube, jubile...) but don't visible off other word on the listbox. It seems like a dictionary.
You can see picture below. (my picture shows two cases, left - hand for typing "j" and right - hand for typing "ju"
Thank you so much. Jan.
Hope to see your reply and forgive me for mistakes to my English.
Jan
Jan il 22 Gen 2019
Modificato: Jan il 22 Gen 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.

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by