Thread Subject: Key characters and read the length of the string problem

Subject: Key characters and read the length of the string problem

From: myh

Date: 9 Jul, 2007 23:55:53

Message: 1 of 5

Hi.

I set the Callback of a edit uicontrol to read the string length of its
String property.
But I found a odd thing that when I get the length of string by keying
characters, calling Callback and executing this command:
   length(get(hObject, 'String'))
the length value is not always the same as the actual length of the string
keying on edit uicontrol.
However, I changed the focus back and forth on edit uicontrol, the length
evaled by above command
became the actual length of string on edit uicontrol.
How do I solve this problem?


Subject: Key characters and read the length of the stri

From: Yair Altman

Date: 9 Jul, 2007 12:22:57

Message: 2 of 5

MYH wrote:
>
>
> Hi.
>
> I set the Callback of a edit uicontrol to read the string length of
> its
> String property.
> But I found a odd thing that when I get the length of string by
> keying characters, calling Callback and executing this command:
> length(get(hObject, 'String'))
> the length value is not always the same as the actual length of the
> string keying on edit uicontrol.
> However, I changed the focus back and forth on edit uicontrol, the
> length evaled by above command
> became the actual length of string on edit uicontrol.
> How do I solve this problem?

Another poster had a similar problem a few months ago (I can't find
the post, though). I believe the problem (which you should report as
a bug to MathWorks) is due to the fact that the 'string' property of
the editbox is only updated after hitting <Enter> or changing
the focus, not during typing.

You could try to call drawnow in your callback. Or, use FindJObj from
the File Exchange to directly access the editbox's string (there's no
problem there - it's updated online during typing).

Yair Altman
 <http://www.ymasoftware.com>

Subject: Key characters and read the length of the stri

From: myh

Date: 10 Jul, 2007 01:03:23

Message: 3 of 5

----- Original Message -----
From: "Yair Altman" <altmanyDEL@gmailDEL.comDEL>
Newsgroups: comp.soft-sys.matlab
Sent: Tuesday, July 10, 2007 12:22 AM
Subject: Re: Key characters and read the length of the stri


> MYH wrote:
>>
>>
>> Hi.
>>
>> I set the Callback of a edit uicontrol to read the string length of
>> its
>> String property.
>> But I found a odd thing that when I get the length of string by
>> keying characters, calling Callback and executing this command:
>> length(get(hObject, 'String'))
>> the length value is not always the same as the actual length of the
>> string keying on edit uicontrol.
>> However, I changed the focus back and forth on edit uicontrol, the
>> length evaled by above command
>> became the actual length of string on edit uicontrol.
>> How do I solve this problem?
>
> Another poster had a similar problem a few months ago (I can't find
> the post, though). I believe the problem (which you should report as
> a bug to MathWorks) is due to the fact that the 'string' property of
> the editbox is only updated after hitting <Enter> or changing
> the focus, not during typing.
>
> You could try to call drawnow in your callback. Or, use FindJObj from

I have tried drawnow in my KeyPressFcn like this:

    drawnow;
    length(get(hObject, 'String'))
    drawnow;

But it seems to me that have no effect.

I also tried findjobj, but its performance was not very good.
I would like more graceful methods.
However, thank for your advises.

> the File Exchange to directly access the editbox's string (there's no
> problem there - it's updated online during typing).
>
> Yair Altman
> <http://www.ymasoftware.com>


Subject: Key characters and read the length of the stri

From: Yair Altman

Date: 9 Jul, 2007 13:29:29

Message: 4 of 5

MYH wrote:
>
> I also tried findjobj, but its performance was not very good.

Use findjobj with the 'nomenu' option - this will significantly
improve performance

Yair Altman
 <http://www.ymasoftware.com>

Subject: Key characters and read the length of the stri

From: myh

Date: 10 Jul, 2007 12:15:17

Message: 5 of 5


"Yair Altman" <altmanyDEL@gmailDEL.comDEL>
???????:ef5ce2f.2@webcrossing.raydaftYaTP...
> MYH wrote:
>>
>> I also tried findjobj, but its performance was not very good.
>
> Use findjobj with the 'nomenu' option - this will significantly
> improve performance
>
> Yair Altman
> <http://www.ymasoftware.com>

I saw some codes of findjobj for getting some ideas and decided to
substisute java components for
MATLAB built-in edit components.
I was successful to achieve reading the actual length of string which
displayed on JTextField object.
This is my part codes (It's a CRC Calculator):

function varargout = CRCCalculator(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
                   'gui_Singleton', gui_Singleton, ...
                   'gui_OpeningFcn', @CRCCalculator_OpeningFcn, ...
                   'gui_OutputFcn', @CRCCalculator_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

    global initialzated;
    % 'initialzated' variable is used to indicate whether the GUI had
    % initialzated, or not.

% --- Executes just before CRCCalculator is made visible.
function CRCCalculator_OpeningFcn(hObject, eventdata, handles, varargin)

    handles.output = hObject;

    % Update handles structure
    guidata(hObject, handles);

    global initialzated;

    % If the GUI had initialzated, we do nothing but return.
    if initialzated == 1
        return;
    end

    % Following codes, we will instatiate java classes for a text field
    % component. We don't use the text field components of MATLAB built-in
    % edit, because it has some bug on KeyPressFCN callback.

    % Import java classes.
    import javax.swing.*;
    import java.awt.*;

    % Set up JTextField GUI component.
    DataIn = JTextField();
    DataIn.setFont(Font('Sans Serif', Font.PLAIN, 16));
    DataIn.setHorizontalAlignment(JTextField.CENTER);
    % Add DataIn component to the figure.
    javacomponent(DataIn, [50 300 650 50]);

    % Add a field for DataIn to handles.
    handles.DataIn = DataIn;
    % Update handles structure
    guidata(hObject, handles);

    % Set the KeyReleasedCallback for DataIn component.
    set(DataIn, 'KeyReleasedCallback', @(hObject,
eventdata)CountBitNum(handles));

    % Clear bits of DataIn to zeros.
    ClearBits(handles);

    % Initialzating completely.
    initialzated = 1;

function CountBitNum(handles)

    % Display the bit count in BitCount component.
    set(handles.BitCount, 'String', length(handles.DataIn.getText()));

function figure1_DeleteFcn(hObject, eventdata, handles)

    % Set initialzated to 0 for next starting GUI.
    global initialzated;
    initialzated = 0;
...


Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com