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:
Get correct string from uicontrol after key press function initiates function execution

Subject: Get correct string from uicontrol after key press function initiates function execution

From: Nate

Date: 14 Sep, 2011 17:30:27

Message: 1 of 2

I kind of butchered that title, but this kind of how my code works,

% create figure with uicontrol edit box with a key press function callback
h1 = figure;
h2 = uicontrol(h1,'Style','Edit','KeyPressFcn',@my_keypressfcn);

% key press function callback
function my_keypressfcn(~,~)
    character = get(h1,'CurrentKey');
    % if the 'return' key is pressed, run the start_computation function
    if strcmp(character,'return')
        start_computation
    end
end

% start computation function
function start_computation(~,~)
    % get string from edit box and convert it to a double
    value = str2double(get(h2,'String'));
    % do other stuff
end

Basically I have an initial gui and when the user hits the 'return' key, I need to get whatever number they typed into that edit box. Unfortunately this is not returning the correct value.

Any suggestions?
Thanks,
Nate

Subject: Get correct string from uicontrol after key press function initiates function execution

From: Nate

Date: 14 Sep, 2011 18:23:28

Message: 2 of 2

"Nate" wrote in message <j4qobj$lb7$1@newscl01ah.mathworks.com>...
> I kind of butchered that title, but this kind of how my code works,
>
> % create figure with uicontrol edit box with a key press function callback
> h1 = figure;
> h2 = uicontrol(h1,'Style','Edit','KeyPressFcn',@my_keypressfcn);
>
> % key press function callback
> function my_keypressfcn(~,~)
> character = get(h1,'CurrentKey');
> % if the 'return' key is pressed, run the start_computation function
> if strcmp(character,'return')
> start_computation
> end
> end
>
> % start computation function
> function start_computation(~,~)
> % get string from edit box and convert it to a double
> value = str2double(get(h2,'String'));
> % do other stuff
> end
>
> Basically I have an initial gui and when the user hits the 'return' key, I need to get whatever number they typed into that edit box. Unfortunately this is not returning the correct value.
>
> Any suggestions?
> Thanks,
> Nate

Apparently throwing a pause in there will do the trick. Maybe Matlab doesn't have enough time to register the value in the edit box when the 'return' key is hit? I don't know, but it worked.

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