Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Edit Box question

Subject: Edit Box question

From: Squall Mania

Date: 29 Nov, 2009 09:14:03

Message: 1 of 24

Hey,

I have created a GUI that has one edit box and a popup menu. I want when a user inputs some texts in the edit box, the popup menu disappears without pressing on Enter Key. Is that possible?

Subject: Edit Box question

From: ImageAnalyst

Date: 29 Nov, 2009 16:32:31

Message: 2 of 24

On Nov 29, 4:14 am, "Squall Mania" <r_sk...@hotmail.com> wrote:
> Hey,
>
> I have created a GUI that has one edit box and a popup menu. I want when a user inputs some texts in the edit box, the popup menu disappears without pressing on Enter Key. Is that possible?

----------------------------------------------------------------------------
The edit box has a keypress callback function. In there, try to use
the set() function to make the popup disappear:
set(handles.popupMenu, 'visible', 'off');

Subject: Edit Box question

From: Squall Mania

Date: 30 Nov, 2009 16:33:22

Message: 3 of 24

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <8e9627b6-0be7-4c3e-aa76-90da4d735848@x15g2000vbr.googlegroups.com>...
> On Nov 29, 4:14?am, "Squall Mania" <r_sk...@hotmail.com> wrote:
> > Hey,
> >
> > I have created a GUI that has one edit box and a popup menu. I want when a user inputs some texts in the edit box, the popup menu disappears without pressing on Enter Key. Is that possible?
>
> ----------------------------------------------------------------------------
> The edit box has a keypress callback function. In there, try to use
> the set() function to make the popup disappear:
> set(handles.popupMenu, 'visible', 'off');

Thanks for the reply, but I don't want to press Enter or any key for that matter. I want at the instant I finish inputting into the edit box, the popup menu disappears.

Thanks again.

Subject: Edit Box question

From: Jan Simon

Date: 2 Dec, 2009 12:00:22

Message: 4 of 24

Dear Squall Mania!

> Thanks for the reply, but I don't want to press Enter or any key for that matter. I want at the instant I finish inputting into the edit box, the popup menu disappears.

How is "finish inputting" defined? Are there any possibilities to detect this event?

Jan

Subject: Edit Box question

From: Squall Mania

Date: 2 Dec, 2009 12:50:04

Message: 5 of 24

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hf5ksl$b6m$1@fred.mathworks.com>...
> Dear Squall Mania!
>
> > Thanks for the reply, but I don't want to press Enter or any key for that matter. I want at the instant I finish inputting into the edit box, the popup menu disappears.
>
> How is "finish inputting" defined? Are there any possibilities to detect this event?
>
> Jan

For example. When I write the word "ACUF", the popup menu automatically detects it and then disappears without a user clicking anywhere on the interface or pressing on Enter key. It's important because some words don't actually need the popup menu so I simply want it to disappear whenever it's needless.

Subject: Edit Box question

From: Steven Lord

Date: 2 Dec, 2009 14:16:14

Message: 6 of 24


"Squall Mania" <r_skeik@hotmail.com> wrote in message
news:hf5nps$9ii$1@fred.mathworks.com...
> "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> <hf5ksl$b6m$1@fred.mathworks.com>...
>> Dear Squall Mania!
>>
>> > Thanks for the reply, but I don't want to press Enter or any key for
>> > that matter. I want at the instant I finish inputting into the edit
>> > box, the popup menu disappears.
>>
>> How is "finish inputting" defined? Are there any possibilities to detect
>> this event?
>>
>> Jan
>
> For example. When I write the word "ACUF", the popup menu automatically
> detects it and then disappears without a user clicking anywhere on the
> interface or pressing on Enter key. It's important because some words
> don't actually need the popup menu so I simply want it to disappear
> whenever it's needless.

Why didn't the popupmenu disappear when you typed "A" or "AC" or "ACU"?
What distinguishes "ACUF" from those other three cases?

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Edit Box question

From: Squall Mania

Date: 2 Dec, 2009 14:49:02

Message: 7 of 24

"Steven Lord" <slord@mathworks.com> wrote in message <hf5sos$ki9$1@fred.mathworks.com>...
>
> "Squall Mania" <r_skeik@hotmail.com> wrote in message
> news:hf5nps$9ii$1@fred.mathworks.com...
> > "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> > <hf5ksl$b6m$1@fred.mathworks.com>...
> >> Dear Squall Mania!
> >>
> >> > Thanks for the reply, but I don't want to press Enter or any key for
> >> > that matter. I want at the instant I finish inputting into the edit
> >> > box, the popup menu disappears.
> >>
> >> How is "finish inputting" defined? Are there any possibilities to detect
> >> this event?
> >>
> >> Jan
> >
> > For example. When I write the word "ACUF", the popup menu automatically
> > detects it and then disappears without a user clicking anywhere on the
> > interface or pressing on Enter key. It's important because some words
> > don't actually need the popup menu so I simply want it to disappear
> > whenever it's needless.
>
> Why didn't the popupmenu disappear when you typed "A" or "AC" or "ACU"?
> What distinguishes "ACUF" from those other three cases?
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>
The word "ACUF" is just an example. The popup menu won't disappear unless a user clicks anywhere on the interface or presses the key "Enter". To clarify more, let's say we have three words:
1. ACUF
2. MAH
3.AAH

What I want is when I type the word "MAH" or "AAH", the popup menu remains visible, but when I type "ACUF", the popup menu automatically disappears without clicking or pressing on anything. Is that possible?

Subject: Edit Box question

From: Yair Altman

Date: 2 Dec, 2009 17:50:04

Message: 8 of 24

"Squall Mania" <r_skeik@hotmail.com> wrote in message <hf5uou$qub$1@fred.mathworks.com>...
> "Steven Lord" <slord@mathworks.com> wrote in message <hf5sos$ki9$1@fred.mathworks.com>...
> >
> > "Squall Mania" <r_skeik@hotmail.com> wrote in message
> > news:hf5nps$9ii$1@fred.mathworks.com...
> > > "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> > > <hf5ksl$b6m$1@fred.mathworks.com>...
> > >> Dear Squall Mania!
> > >>
> > >> > Thanks for the reply, but I don't want to press Enter or any key for
> > >> > that matter. I want at the instant I finish inputting into the edit
> > >> > box, the popup menu disappears.
> > >>
> > >> How is "finish inputting" defined? Are there any possibilities to detect
> > >> this event?
> > >>
> > >> Jan
> > >
> > > For example. When I write the word "ACUF", the popup menu automatically
> > > detects it and then disappears without a user clicking anywhere on the
> > > interface or pressing on Enter key. It's important because some words
> > > don't actually need the popup menu so I simply want it to disappear
> > > whenever it's needless.
> >
> > Why didn't the popupmenu disappear when you typed "A" or "AC" or "ACU"?
> > What distinguishes "ACUF" from those other three cases?
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> >
> The word "ACUF" is just an example. The popup menu won't disappear unless a user clicks anywhere on the interface or presses the key "Enter". To clarify more, let's say we have three words:
> 1. ACUF
> 2. MAH
> 3.AAH
>
> What I want is when I type the word "MAH" or "AAH", the popup menu remains visible, but when I type "ACUF", the popup menu automatically disappears without clicking or pressing on anything. Is that possible?

Yes it is possible, but you will need to use Java popupmenus (javax.swing.JComboBox or JTextField objects to be exact) - it can't be done with standard Matlab. To use the Java objects, either get the handle of the one which underlies the Matlab control (via the FindJObj utility on the File Exchange) or create a new one from scratch and then add it to your GUI using the javacomponent function (you can also use the uicomponent or jcontrol helper utilities on the File Exchange).

Remember to set the object's Editable property value to 1 (=on). Then set a callback function to the KeyTypedCallback property (or any of the other similar properties). This callback function (a Matlab function - it doesn't need to be written in Java) should hold the logic controlling the popup-menu's contents and visibility. It will be called after every single keystroke.

More info:
http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html
http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/
http://undocumentedmatlab.com/blog/uicontrol-callbacks/

HTH,
Yair Altman
http://UndocumentedMatlab.com

Subject: Edit Box question

From: Matt Fig

Date: 2 Dec, 2009 20:18:03

Message: 9 of 24

I am not sure this cannot be done in MATLAB alone, unless I misunderstand the requirement. Here is an example:





function [] = mygui()

S.KEY = [];
S.fh = figure('units','pixels',...
    'position',[300 300 300 110],...
    'menubar','none',...
    'name','GUI_17',...
    'numbertitle','off',...
    'resize','off');
S.pp = uicontrol('style','pop',...
    'units','pixels',...
    'position',[20 10 260 40],...
    'string',{'ACUF','MAH','AHH'});
S.ed = uicontrol('style','edit',...
    'units','pix',...
    'position',[20 60 260 30],...
    'fontsize',16,'string','one');

set(S.ed,'keypressfcn',{@ed_kpfcn},'callback',{@ed_call})


    function [] = ed_kpfcn(varargin)
        % Keypressfcn for the editbox.
        E = varargin{2};

        if strcmp(E.Key,'return')
            return
        end

        if strcmp(E.Key,'backspace')
            S.KEY = S.KEY(1:end-1)
            if strcmp(S.KEY,'ACUF')
                set(S.pp,'visible','off')
            elseif isempty(get(S.ed,'string'))
                S.KEY = '';
                set(S.pp,'visible','on')
            else
                set(S.pp,'visible','on')
            end
            return
        end

        S.KEY = [S.KEY E.Character];

        if strcmp(S.KEY,'ACUF')
            set(S.pp,'visible','off')
        elseif isempty(get(S.ed,'string'))
            S.KEY = '';
            set(S.pp,'visible','on')
        else
            set(S.pp,'visible','on')
        end
    end

    function [] = ed_call(varargin)
        % Callback for the editbox.
    end
end




Note that the user can enter ACUF, and the popup disappears. If the user keeps typing, the popup comes back. Now if the user backspaces through until only ACUF is showing, the popup disappears again. Of course if I were trying to think of all the maniacal ways a user could ruin this, I would have to come up with a way to deal with the user using the arrow keys to insert/delete a character in the middle, but this seems a good start.

Subject: Edit Box question

From: Squall Mania

Date: 3 Dec, 2009 14:54:18

Message: 10 of 24

"Matt Fig" <spamanon@yahoo.com> wrote in message <hf6i1r$7m0$1@fred.mathworks.com>...
> I am not sure this cannot be done in MATLAB alone, unless I misunderstand the requirement. Here is an example:
>
>
>
>
>
> function [] = mygui()
>
> S.KEY = [];
> S.fh = figure('units','pixels',...
> 'position',[300 300 300 110],...
> 'menubar','none',...
> 'name','GUI_17',...
> 'numbertitle','off',...
> 'resize','off');
> S.pp = uicontrol('style','pop',...
> 'units','pixels',...
> 'position',[20 10 260 40],...
> 'string',{'ACUF','MAH','AHH'});
> S.ed = uicontrol('style','edit',...
> 'units','pix',...
> 'position',[20 60 260 30],...
> 'fontsize',16,'string','one');
>
> set(S.ed,'keypressfcn',{@ed_kpfcn},'callback',{@ed_call})
>
>
> function [] = ed_kpfcn(varargin)
> % Keypressfcn for the editbox.
> E = varargin{2};
>
> if strcmp(E.Key,'return')
> return
> end
>
> if strcmp(E.Key,'backspace')
> S.KEY = S.KEY(1:end-1)
> if strcmp(S.KEY,'ACUF')
> set(S.pp,'visible','off')
> elseif isempty(get(S.ed,'string'))
> S.KEY = '';
> set(S.pp,'visible','on')
> else
> set(S.pp,'visible','on')
> end
> return
> end
>
> S.KEY = [S.KEY E.Character];
>
> if strcmp(S.KEY,'ACUF')
> set(S.pp,'visible','off')
> elseif isempty(get(S.ed,'string'))
> S.KEY = '';
> set(S.pp,'visible','on')
> else
> set(S.pp,'visible','on')
> end
> end
>
> function [] = ed_call(varargin)
> % Callback for the editbox.
> end
> end
>
>
>
>
> Note that the user can enter ACUF, and the popup disappears. If the user keeps typing, the popup comes back. Now if the user backspaces through until only ACUF is showing, the popup disappears again. Of course if I were trying to think of all the maniacal ways a user could ruin this, I would have to come up with a way to deal with the user using the arrow keys to insert/delete a character in the middle, but this seems a good start.

That's exactly what I want! I simply copied the code into m-file and it worked great. However, when I created a Blank GUI (Default), and tried to copy the code into the m-file, I received many errors. I was trying hard to make it work, but I couldn't since the functions are a bit different than what I have and I don't know which code should be copied into which function. Here is a sample m-file created from Guide:


function varargout = sample(varargin)

function sample_OptionalFcn (hObject, eventdata, handles, varargin)

function edit1_Callback (hObject, eventdata, handles)

function popupmenu1_Callback (hObject, eventdata, handles)

function popupmenu1_CreateFcn (hObject, eventdata, handles)

As shown above, there are five functions. Can you tell me how I can distribute the code you wrote to the above functions?

Thanks a lot!
Looking forward to hearing from you.

Subject: Edit Box question

From: Squall Mania

Date: 3 Dec, 2009 15:10:13

Message: 11 of 24

"Squall Mania" <r_skeik@hotmail.com> wrote in message <hf8jeq$ctg$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hf6i1r$7m0$1@fred.mathworks.com>...
> > I am not sure this cannot be done in MATLAB alone, unless I misunderstand the requirement. Here is an example:
> >
> >
> >
> >
> >
> > function [] = mygui()
> >
> > S.KEY = [];
> > S.fh = figure('units','pixels',...
> > 'position',[300 300 300 110],...
> > 'menubar','none',...
> > 'name','GUI_17',...
> > 'numbertitle','off',...
> > 'resize','off');
> > S.pp = uicontrol('style','pop',...
> > 'units','pixels',...
> > 'position',[20 10 260 40],...
> > 'string',{'ACUF','MAH','AHH'});
> > S.ed = uicontrol('style','edit',...
> > 'units','pix',...
> > 'position',[20 60 260 30],...
> > 'fontsize',16,'string','one');
> >
> > set(S.ed,'keypressfcn',{@ed_kpfcn},'callback',{@ed_call})
> >
> >
> > function [] = ed_kpfcn(varargin)
> > % Keypressfcn for the editbox.
> > E = varargin{2};
> >
> > if strcmp(E.Key,'return')
> > return
> > end
> >
> > if strcmp(E.Key,'backspace')
> > S.KEY = S.KEY(1:end-1)
> > if strcmp(S.KEY,'ACUF')
> > set(S.pp,'visible','off')
> > elseif isempty(get(S.ed,'string'))
> > S.KEY = '';
> > set(S.pp,'visible','on')
> > else
> > set(S.pp,'visible','on')
> > end
> > return
> > end
> >
> > S.KEY = [S.KEY E.Character];
> >
> > if strcmp(S.KEY,'ACUF')
> > set(S.pp,'visible','off')
> > elseif isempty(get(S.ed,'string'))
> > S.KEY = '';
> > set(S.pp,'visible','on')
> > else
> > set(S.pp,'visible','on')
> > end
> > end
> >
> > function [] = ed_call(varargin)
> > % Callback for the editbox.
> > end
> > end
> >
> >
> >
> >
> > Note that the user can enter ACUF, and the popup disappears. If the user keeps typing, the popup comes back. Now if the user backspaces through until only ACUF is showing, the popup disappears again. Of course if I were trying to think of all the maniacal ways a user could ruin this, I would have to come up with a way to deal with the user using the arrow keys to insert/delete a character in the middle, but this seems a good start.
>
> That's exactly what I want! I simply copied the code into m-file and it worked great. However, when I created a Blank GUI (Default), and tried to copy the code into the m-file, I received many errors. I was trying hard to make it work, but I couldn't since the functions are a bit different than what I have and I don't know which code should be copied into which function. Here is a sample m-file created from Guide:
>
>
> function varargout = sample(varargin)
>
> function sample_OptionalFcn (hObject, eventdata, handles, varargin)
>
> function edit1_Callback (hObject, eventdata, handles)
>
> function popupmenu1_Callback (hObject, eventdata, handles)
>
> function popupmenu1_CreateFcn (hObject, eventdata, handles)
>
> As shown above, there are five functions. Can you tell me how I can distribute the code you wrote to the above functions?
>
> Thanks a lot!
> Looking forward to hearing from you.

Sorry, here are the correct number of functions. There are six:

function varargout = sample(varargin)

function sample_OptionalFcn (hObject, eventdata, handles, varargin)

function varargout = sample_OutputFcn (hObject, eventdata, handles)
 
function edit1_Callback (hObject, eventdata, handles)
 
function popupmenu1_Callback (hObject, eventdata, handles)
 
function popupmenu1_CreateFcn (hObject, eventdata, handles)

Thanks.

Subject: Edit Box question

From: Matt Fig

Date: 3 Dec, 2009 19:18:04

Message: 12 of 24

It looks like you are using GUIDE to generate your GUI. As such there are a few changes you will have to make. I store all my data in a structure called S, and since I am using nested functions there is no need to update the structure after every change in S. For your case, you need to add a keypressfcn for your editbox which has basically the code I showed, except that your variable names may be different. Also at the end you will have to update the data structure in whatever way you are using (userdata, appdata, guidata, etc...). What I call E, GUIDE usually calls EventData if I remember correctly and am not too outdated.

Good luck.

Subject: Edit Box question

From: Squall Mania

Date: 4 Dec, 2009 21:04:09

Message: 13 of 24

"Matt Fig" <spamanon@yahoo.com> wrote in message <hf92tc$sth$1@fred.mathworks.com>...
> It looks like you are using GUIDE to generate your GUI. As such there are a few changes you will have to make. I store all my data in a structure called S, and since I am using nested functions there is no need to update the structure after every change in S. For your case, you need to add a keypressfcn for your editbox which has basically the code I showed, except that your variable names may be different. Also at the end you will have to update the data structure in whatever way you are using (userdata, appdata, guidata, etc...). What I call E, GUIDE usually calls EventData if I remember correctly and am not too outdated.
>
> Good luck.

When I ran your code using a regular m-file, and tried to input the letter "a", KEY equaled "a" in the workspace. Then, when I continued inputting the letter "b", KEY equaled "ab" in the workspace and so on. I tried to make a few changes to the code since I'm using GUIDE, and I ended up with the following:

function varargout = sample1(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
                   'gui_Singleton', gui_Singleton, ...
                   'gui_OpeningFcn', @sample1_OpeningFcn, ...
                   'gui_OutputFcn', @sample1_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

function sample1_OpeningFcn(hObject, eventdata, handles, varargin)

handles.KEY=[];

set(handles.figure1,'KeyPressFcn',@myFunction);
set(handles.edit1,'KeyPressFcn',{@myFunction},'callback',{@edit1_Callback});

handles.output = hObject;

guidata(hObject, handles);


function varargout = sample1_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;

function edit1_Callback(hObject, eventdata, handles)

function edit1_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit2_Callback(hObject, eventdata, handles)

function edit2_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function myFunction(src,eventdata)

handles = guidata(src);

k= eventdata.Key;
 
if strcmp(k,'return')
    pause(0.01)
 
    return
end
    
if strcmp(k,'backspace')

    handles.KEY= handles.KEY(1:end-1)

    if strcmpi(handles.KEY,'ACUF')
        set(handles.edit2,'visible','off')
    elseif isempty(get(handles.edit1,'string'))
        handles.KEY='';
         set(handles.edit2,'visible','on')
    else
         set(handles.edit2,'visible','on')
    end
    return
end

handles.KEY = [handles.KEY eventdata.Character]


if strcmpi(handles.KEY,'ACUF')
   set(handles.edit2,'visible','off')
   elseif isempty(get(handles.edit1,'string'))
       handles.KEY='';
      set(handles.edit2,'visible','on')
else
            set(handles.edit2,'visible','on')
end

The code was run without any errors; however, when I typed the letter "a", KEY equaled "a", but when I continued typing the letter "b", KEY equaled "b" only without showing the previous letter. Is there someting I'm missing?

Thanks.

Subject: Edit Box question

From: Squall Mania

Date: 4 Dec, 2009 21:51:05

Message: 14 of 24

"Squall Mania" <r_skeik@hotmail.com> wrote in message <hfbtg8$39n$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hf92tc$sth$1@fred.mathworks.com>...
> > It looks like you are using GUIDE to generate your GUI. As such there are a few changes you will have to make. I store all my data in a structure called S, and since I am using nested functions there is no need to update the structure after every change in S. For your case, you need to add a keypressfcn for your editbox which has basically the code I showed, except that your variable names may be different. Also at the end you will have to update the data structure in whatever way you are using (userdata, appdata, guidata, etc...). What I call E, GUIDE usually calls EventData if I remember correctly and am not too outdated.
> >
> > Good luck.
>
> When I ran your code using a regular m-file, and tried to input the letter "a", KEY equaled "a" in the workspace. Then, when I continued inputting the letter "b", KEY equaled "ab" in the workspace and so on. I tried to make a few changes to the code since I'm using GUIDE, and I ended up with the following:
>
> function varargout = sample1(varargin)
>
> gui_Singleton = 1;
> gui_State = struct('gui_Name', mfilename, ...
> 'gui_Singleton', gui_Singleton, ...
> 'gui_OpeningFcn', @sample1_OpeningFcn, ...
> 'gui_OutputFcn', @sample1_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
>
> function sample1_OpeningFcn(hObject, eventdata, handles, varargin)
>
> handles.KEY=[];
>
> set(handles.figure1,'KeyPressFcn',@myFunction);
> set(handles.edit1,'KeyPressFcn',{@myFunction},'callback',{@edit1_Callback});
>
> handles.output = hObject;
>
> guidata(hObject, handles);
>
>
> function varargout = sample1_OutputFcn(hObject, eventdata, handles)
>
> varargout{1} = handles.output;
>
> function edit1_Callback(hObject, eventdata, handles)
>
> function edit1_CreateFcn(hObject, eventdata, handles)
>
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
> function edit2_Callback(hObject, eventdata, handles)
>
> function edit2_CreateFcn(hObject, eventdata, handles)
>
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
> function myFunction(src,eventdata)
>
> handles = guidata(src);
>
> k= eventdata.Key;
>
> if strcmp(k,'return')
> pause(0.01)
>
> return
> end
>
> if strcmp(k,'backspace')
>
> handles.KEY= handles.KEY(1:end-1)
>
> if strcmpi(handles.KEY,'ACUF')
> set(handles.edit2,'visible','off')
> elseif isempty(get(handles.edit1,'string'))
> handles.KEY='';
> set(handles.edit2,'visible','on')
> else
> set(handles.edit2,'visible','on')
> end
> return
> end
>
> handles.KEY = [handles.KEY eventdata.Character]
>
>
> if strcmpi(handles.KEY,'ACUF')
> set(handles.edit2,'visible','off')
> elseif isempty(get(handles.edit1,'string'))
> handles.KEY='';
> set(handles.edit2,'visible','on')
> else
> set(handles.edit2,'visible','on')
> end
>
> The code was run without any errors; however, when I typed the letter "a", KEY equaled "a", but when I continued typing the letter "b", KEY equaled "b" only without showing the previous letter. Is there someting I'm missing?
>
> Thanks.

Problem solved! I forgot to update the data structure using guidata. Thanks a lot for your help.

Subject: Edit Box question

From: Travis

Date: 14 Apr, 2010 17:37:04

Message: 15 of 24

What was your fix on this? I am trying to so the same thing you are and I can't figure it out.

Subject: Edit Box question

From: Matt Fig

Date: 14 Apr, 2010 20:13:05

Message: 16 of 24

"Travis " <sinusoid2@hotmail.com> wrote in message <hq4ug0$6gq$1@fred.mathworks.com>...
> What was your fix on this? I am trying to so the same thing you are and I can't figure it out.

Did you actually *read* the whole thread?

Subject: Edit Box question

From: Travis

Date: 14 Apr, 2010 21:28:04

Message: 17 of 24

"Matt Fig" <spamanon@yahoo.com> wrote in message <hq57kh$bq0$1@fred.mathworks.com>...
> "Travis " <sinusoid2@hotmail.com> wrote in message <hq4ug0$6gq$1@fred.mathworks.com>...
> > What was your fix on this? I am trying to so the same thing you are and I can't figure it out.
>
> Did you actually *read* the whole thread?

yes I *read* the whole thread. Using the code provided I can either get the same issue, not saving the whole string, or I can get it to save the whole string, but it loses handles.edit1 for some reason when I add
handles.KEY = get(hObject,'string')
guidata(hObject, handles);
to the edit1_Callback function

Subject: Edit Box question

From: Travis

Date: 14 Apr, 2010 22:37:04

Message: 18 of 24

got it, nevermind....

Subject: Edit Box question

From: Matt Fig

Date: 26 May, 2010 16:43:22

Message: 19 of 24

There is a subtle error in the gui example above. It was brought to my attention by someone who found this old thread and tried to implement the code. Since this might happen again, I am updating the code. I only hope that future readers read all the way to the last post!





function [] = mygui()
% If user types ACUF in the editbox, the popupmenu disappears.
S.KEY = []; % This will store the contents of editbox.
S.fh = figure('units','pixels',...
    'position',[300 300 300 110],...
    'menubar','none',...
    'name','my_gui',...
    'numbertitle','off',...
    'resize','off');
S.pp = uicontrol('style','pop',...
    'units','pixels',...
    'position',[20 10 260 40],...
    'string',{'ACUF','MAH','AHH'});
S.ed = uicontrol('style','edit',...
    'units','pix',...
    'position',[20 60 260 30],...
    'fontsize',16);

set(S.ed,'keypressfcn',{@ed_kpfcn})
uicontrol(S.ed)

    function [] = ed_kpfcn(varargin)
        % Keypressfcn for the editbox.
        E = varargin{2};

        if strcmp(E.Key,'return')
            return
        end
        
        if strcmp(E.Key,'backspace')
            S.KEY = S.KEY(1:end-1);
            if strcmp(S.KEY,'ACUF')
                set(S.pp,'visible','off')
            else
                set(S.pp,'visible','on')
            end
            return
        end

        S.KEY = [S.KEY E.Character];

        if strcmp(S.KEY,'ACUF')
            set(S.pp,'visible','off')
        else
            set(S.pp,'visible','on')
        end
    end
end

Subject: Edit Box question

From: Mathias Benedek

Date: 21 Sep, 2010 14:43:20

Message: 20 of 24

Why is it actually not possible to read out the content of the edit field directly (instead of recording all single keys and backspaces)?
I tried it using

S.key = get(S.ed, 'String');

in the keypress function but this does not work correctly. This command always returns the original content of the edit field even after it was changed. Does anybody know why this is so?

Moreover, in the debugging mode the line above returns the correct content of the edit field!?

Thank you,
Mathias

Subject: Edit Box question

From: Yair Altman

Date: 21 Sep, 2010 16:35:29

Message: 21 of 24

"Mathias Benedek" <mathias.benedek@uni-graz.at> wrote in message <i7aga8$t9f$1@fred.mathworks.com>...
> Why is it actually not possible to read out the content of the edit field directly (instead of recording all single keys and backspaces)?
> I tried it using
>
> S.key = get(S.ed, 'String');
>
> in the keypress function but this does not work correctly. This command always returns the original content of the edit field even after it was changed. Does anybody know why this is so?
>
> Moreover, in the debugging mode the line above returns the correct content of the edit field!?
>
> Thank you,
> Mathias


You can do this by accessing the edit control's underlying Java component:

1. First download FindJObj from the Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects

You can read a technical description of FindJObj here: http://UndocumentedMatlab.com/blog/findjobj-find-underlying-java-object/

2. Next use the findjobj utility on your edit field's handle:

jEdit = findjobj(S.ed);

3. Now read the text from directly the java object:

text = jEdit.getText; % or: text = get(jEdit,'Text');

4. Related articles:
http://UndocumentedMatlab.com/blog/uicontrol-callbacks/
http://UndocumentedMatlab.com/blog/rich-matlab-editbox-contents/
http://UndocumentedMatlab.com/blog/customizing-listbox-editbox-scrollbars/
http://UndocumentedMatlab.com/blog/setting-line-position-in-edit-box-uicontrol/

Yair Altman
http://UndocumentedMatlab.com

Subject: Edit Box question

From: Oleg Komarov

Date: 15 Dec, 2010 20:30:23

Message: 22 of 24

"Mathias Benedek" <mathias.benedek@uni-graz.at> wrote in message <i7aga8$t9f$1@fred.mathworks.com>...
> Why is it actually not possible to read out the content of the edit field directly (instead of recording all single keys and backspaces)?
> I tried it using
>
> S.key = get(S.ed, 'String');
>
> in the keypress function but this does not work correctly. This command always returns the original content of the edit field even after it was changed. Does anybody know why this is so?
>
> Moreover, in the debugging mode the line above returns the correct content of the edit field!?
>
> Thank you,
> Mathias

I have exactly the same problem!

I am trying to learn GUI from Matt Fig's submission and this behaviour is really annoying since I don't understand (as I am a novice with GUI) if it's a bug or my lack of knowledge...but from what I read on the thread it seems more likely a bug...and I don't want to dig into java yet cause I feel uneasy.

Oleg

Subject: Edit Box question

From: Echidna

Date: 15 Feb, 2013 12:59:07

Message: 23 of 24

"Yair Altman" wrote in message <i7amsh$7ln$1@fred.mathworks.com>...

> You can do this by accessing the edit control's underlying Java component:
>
> 1. First download FindJObj from the Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects
>
> You can read a technical description of FindJObj here: http://UndocumentedMatlab.com/blog/findjobj-find-underlying-java-object/
>
> 2. Next use the findjobj utility on your edit field's handle:
>
> jEdit = findjobj(S.ed);
>
> 3. Now read the text from directly the java object:
>
> text = jEdit.getText; % or: text = get(jEdit,'Text');
>
>
> Yair Altman
> http://UndocumentedMatlab.com

thanks, this really works perfectly to read in the text that is in the edit box without pressing enter to update the String of this box, BUT, one problem remains, how can you check if the text in the edit box has been changed by the user (if this occurs than a function needs to be performed). I have tried with the addlistener as is suggested for continuously updating value of a slider, but this does not work for the edit text box as the value is not changed continuously...

I was thinking to maybe add a function that executes every time any key is pressed but under which callback do I have to place such a function???

Subject: Edit Box question

From: Yair Altman

Date: 16 Feb, 2013 16:38:05

Message: 24 of 24

"Echidna" wrote in message <kflbeq$3vj$1@newscl01ah.mathworks.com>...
> "Yair Altman" wrote in message <i7amsh$7ln$1@fred.mathworks.com>...
>
> > You can do this by accessing the edit control's underlying Java component:
> >
> > 1. First download FindJObj from the Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects
> >
> > You can read a technical description of FindJObj here: http://UndocumentedMatlab.com/blog/findjobj-find-underlying-java-object/
> >
> > 2. Next use the findjobj utility on your edit field's handle:
> >
> > jEdit = findjobj(S.ed);
> >
> > 3. Now read the text from directly the java object:
> >
> > text = jEdit.getText; % or: text = get(jEdit,'Text');
> >
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
>
> thanks, this really works perfectly to read in the text that is in the edit box without pressing enter to update the String of this box, BUT, one problem remains, how can you check if the text in the edit box has been changed by the user (if this occurs than a function needs to be performed). I have tried with the addlistener as is suggested for continuously updating value of a slider, but this does not work for the edit text box as the value is not changed continuously...
>
> I was thinking to maybe add a function that executes every time any key is pressed but under which callback do I have to place such a function???


Look at KeyPressedCallback or KeyTypedCallback. See here: http://undocumentedmatlab.com/blog/uicontrol-callbacks/

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us