Callback to edit box to update variable

조회 수: 13 (최근 30일)
Mckenna Relling
Mckenna Relling 2022년 2월 21일
답변: Voss 2022년 2월 22일
I have begun my program by defining a variable Ground, where Ground=40. I have a ui text field corresponding to this variable, and when a user edits the text field by typing a number, I want my callback function to update the value of the Ground variable in my workspace. How can this be done? Below is what I have written, which does not work.
Ground=40;
ground_control=uicontrol('Style','edit', 'String', num2str(Ground), 'Position', [315, 70, 70, 20], 'Callback', {@change_ground});
function [Ground] =change_ground(hObject, eventdata, handles)
Ground=str2double(hObject);
end

답변 (1개)

Voss
Voss 2022년 2월 22일
It's not typically necessary (or a good idea) to have data in a UI mirrored in the base workspace, but here's how you can do it (for educational purposes).
Ground=40;
ground_control=uicontrol('Style','edit', 'String', num2str(Ground), 'Position', [315, 70, 70, 20], 'Callback', {@change_ground});
function change_ground(hObject, eventdata, handles)
Ground = str2double(get(hObject,'String'));
assignin('base','Ground',Ground);
end
Typically your uicontrol would be part of a larger GUI that would be handling all the data, so that's why you wouldn't need to do this.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by