How to display data from editext in listbox in GUI ?

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

This will not work with a standard uicontrol as "edit text" element, because it does not trigger a callback during the string is typed. But you can use the WindowsKeyPressFcn to let it look like the text is directly inserted in the textfield. Please explain, if you use GUIDE, AppDesigner or create the GUI programmatically.
to add onto this and possibly save you some time, if you would like the keypress to be the enter key, the char equivalent would be char(13)
char(13) is the return key, but these days the "enter" key on keyboards is generally the newline character, char(10)
Le Dung
Le Dung il 17 Gen 2019
Modificato: Le Dung il 17 Gen 2019
Thank you so much. Jan,Kevin Phung and Walter Roberson
But, how to use char(10)?
I do not understand, what "the keypress to be the enter key" means.
Dear Jan,
Could you explain more detail about how to use WindowsKeyPressFcn for my problem?

Accedi per commentare.

 Risposta accettata

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

strncmpi() might be better, or startWith().
You're right! I forgot about those.
thank you so much.
I set "tag" for editbox is "inputword" and "listword" for listbox
in function_callback of editbox i wrote:
input = get(handles.inputword,'string')
output = get(handles.listword,'string')
strncmpi(output,input)
But, when i use strncmpi(output,input), matlba returns:
Error using strncmpi
Not enough input arguments.
strncmpi(strings,strings,N)
So, Should i choise how many value of N is?
strncmpi(input, output, length(input))
@Le Dung: See: doc strncmpi. The command strncmpi(A, B, N) compares the first N characters of the string(s) A and B. So use Walter's suggestion.
Oh, great, it works fine. But, i haven't solved my problem: that is:
when i import word "j" on edittext, the listbox will display all word that begin with word "j". Similarly, when i type a consecutive word, "a", (now, i'm having "ja" on edittext).
How to matlab realize that when i have typed "j", matlab run "enter" key automatically.
If you configure a KeyPressFcn for the uicontrol style edit, then it will be triggered for each key pressed, even though the user has not pressed enter yet.
The difficulty is that the String property of the uicontrol will not be updated until the user presses enter or moves the focus to somewhere else. You therefore need to keep track of the individual key presses yourself, and you need to manage character deletion yourself.
Le Dung
Le Dung il 18 Gen 2019
Modificato: Le Dung il 18 Gen 2019
Yes. Thank you so much.
I know. but i don't know how to use KeyPressFcn for my problem
After, i have had a logical array (1 - 0). how to display words that have logical value equal to 1 in listbox?
example of logical indexing:
a = {'a','b','c','d'};
% if b is a logical index, say,
b = logical([1 0 0 1]);
%then
a(b)
%would return
ans =
1×2 cell array
{'a'} {'d'}
Le Dung
Le Dung il 19 Gen 2019
Modificato: Le Dung il 19 Gen 2019
OK thank you so much.
By the way, are you Vietnamese? Kevin Phung
Nope, Chinese. But my parents are from there so vietnamese culture is heavily a part of mine!
Oh. Sound good

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

Yes. i see
Thank you so much Jan
Dear Jan .
It's not work.
this is all my code:
function varargout = Tudien2(varargin)
% TUDIEN2 MATLAB code for Tudien2.fig
% TUDIEN2, by itself, creates a new TUDIEN2 or raises the existing
% singleton*.
%
% H = TUDIEN2 returns the handle to a new TUDIEN2 or the handle to
% the existing singleton*.
%
% TUDIEN2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TUDIEN2.M with the given input arguments.
%
% TUDIEN2('Property','Value',...) creates a new TUDIEN2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Tudien2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Tudien2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Tudien2
% Last Modified by GUIDE v2.5 18-Jan-2019 10:33:21
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Tudien2_OpeningFcn, ...
'gui_OutputFcn', @Tudien2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Tudien2 is made visible.
function Tudien2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Tudien2 (see VARARGIN)
% Choose default command line output for Tudien2
handles.output = hObject;
set(gcf,'KeyPressFcn',@myKeyPress)
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Tudien2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Tudien2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function inputword_Callback(hObject, eventdata, handles)
% hObject handle to inputword (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of inputword as text
% str2double(get(hObject,'String')) returns contents of inputword as a double
% --- Executes during object creation, after setting all properties.
function inputword_CreateFcn(hObject, eventdata, handles)
% hObject handle to inputword (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in listword.
function listword_Callback(hObject, eventdata, handles)
% hObject handle to listword (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listword contents as cell array
% contents{get(hObject,'Value')} returns selected item from listword
% --- Executes during object creation, after setting all properties.
function listword_CreateFcn(hObject, eventdata, handles)
% hObject handle to listword (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function myKeyPress(hObject,EventData,handles)
StrEditbox = get(handles.inputword,'String')
StrListbox = get(handles.listword,'String')
logic = strncmpi(StrEditbox,StrListbox,length(StrEditbox)) % returns a logical array
Match = find(logic) % returns a vector whose each element is poisition of non - zeros element
% in variable "logic"
switch EventData.Key
case 'return'
% If only 1 string in the string list is matching,
% RETURN copies it to the edit field:
if numel(Match) == 1
set(handles.inputword,'String', StrListbox{Match});
end
case 'backspace'
if ~isempty(StrEditbox)
StrEditbox(end) = [];
end
case 'delete'
... ???
% case Arrow keys?!
otherwise
c = EventData.Character;
if ~isempty(c)
StrEditbox = [StrEditbox,c];
end
end
set(handles.inputword, 'String', StrEditbox);
Match = find(strncmpi(StrEditbox, StrListbox, length(StrEditbox)));
set(handles.listword,'Value', Match); % Select all matching strings in the list
if isempty(Match) % Move fiorst selected string to top
set(handles.listword, 'ListBoxTop', 1);
else
set(handles.listword, 'ListBoxTop', Match(1));
end
And matlab returns:
Error using feval
Undefined function 'inputword_KeyPressFcn' for input arguments of type 'matlab.ui.control.UIControl'.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Tudien2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Tudien2('inputword_KeyPressFcn',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl KeyPressFcn
Jan
Jan il 21 Gen 2019
Modificato: Jan il 22 Gen 2019
You have created the function myKeyPress and activated it by:
set(gcf,'KeyPressFcn',@myKeyPress)
The error message means, that the GUI searchs for inputword_KeyPressFcn. Try to rename "myKeyPress" to "inputword_KeyPressFcn".
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

Richiesto:

il 16 Gen 2019

Modificato:

Jan
il 22 Gen 2019

Community Treasure Hunt

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

Start Hunting!

Translated by