Getting numerical value from edit box in MATLAB

10,392

You can get the value of the edit box with

S = get(editBoxHandle, 'string');

If it's a numerical value, then convert it

N = str2num(S);

If you want to have only the digits within a string mixing letters and numbers, this code

S = '123abc456xyz';
N = cell2mat(regexp(S, '\d+', 'match')); 
disp(N)

gives N=123456 (from this SO answer).

Share:
10,392
Chethan
Author by

Chethan

Updated on June 04, 2022

Comments

  • Chethan
    Chethan almost 2 years

    I've designed a GUI with resize option in it. Where user is allowed to enter image size in 2 provided edit boxes.

    function x_Callback(hObject, eventdata, handles)
    % hObject    handle to x (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    user_entry_X = str2double(get(hObject,'string'));
    if isnan(user_entry_X)
     errordlg('You must enter a numeric value','Bad Input','modal')
     uicontrol(hObject)
    return
    end
    

    above code is for edit box X. If user gives non-numerical input it results in error. But I'm not getting how to fetch entered numeric values. I've a pushbutton named resize, on pressing that after entering numbers in edit box image should get resize. What should i use in my resize_callback function? please help me.

  • Chethan
    Chethan over 10 years
    Thank you, well how can i clear the same edit box? I've a push button with name reset. after pressing that edit box should show zero values in the sense nothing should present.
  • marsei
    marsei over 10 years
    The twin function of get will give you the control of the edit box content: set(editBoxHandle, 'string','') where '' stands for the empty string.