MATLAB Answers

How to make two numeric field related in GUI?

조회 수: 1(최근 30일)
Mohamed Elasmar
Mohamed Elasmar 16 Sep 2019
답변: Ned 20 Sep 2019
It is the first time for me to use MATLAB GUI and I am trying to make a relation between two numeric field componets, so that if I put a number in the first numeric field and press "Enter", a specific value will be shown in the other numeric field according to an equation that I have.
Could someone please help me to do this?

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Johannes Fischer
Johannes Fischer 16 Sep 2019
The 'Callback' property of the uicontrol defines which function to call when you press enter while the edit field is in focus. A small example:
function minimalUI
figure('Position', [300 300 500 150])
editA = uicontrol('style', 'edit', ...
'Position', [100 50 100 50], ...
'Callback', {@cbA});
editB = uicontrol('style', 'edit', ...
'Position', [300 50 100 50], ...
'Callback', {@cbB});
function cbA(src, evtData)
set(editB, 'String', get(src, 'String'));
function cbB(src, evtData)
set(editA, 'String', get(src, 'String'));
Your callback function always needs two input arguments, the first is the handle to the calling object and the second tells you something about what happend. That means you must always define your callback functions with at least tqo input arguments, whether you need them or not.

  댓글 수: 4

표시 이전 댓글 수: 1
Johannes Fischer
Johannes Fischer 17 Sep 2019
Assuming your left edit field is still called editA and the right edit field is still called editB, then you can rewrite the callback function for editA:
function cbA(src, evtData)
Molality_NaOH = % ... the formula you want to have
% Since the edit field actually accepts strings instead of numbers,
% we need to convert the result into a string first, using num2str.
set(editB, 'String', num2str(Molality_NaOH));
If this is the only interaction you need, you can also remove the callback function from editB
editB = uicontrol('style', 'edit', ...
'Position', [300 50 100 50]);
Mohamed Elasmar
Mohamed Elasmar 17 Sep 2019
Actually, I am using App Designer and I am trying to change the callbacks in the same way of your guide, but it seems that they are fixed and cannot be changed.
Are you using App Designer also?
I am sorry if do not understand well what you explains, but as I said that I am using MATLAB GUI for the first time and I am not familiar with all its functions and Syntax.
Rik 17 Sep 2019
This page should help. AppDesigner works similar to a class, while this example code has a more functional syntax.

댓글을 달려면 로그인하십시오.

Ned 20 Sep 2019
Assuming the numeric fields are EditFieldA and EditFieldB, and you want FieldB to display double the value of FieldA,
make this the value changed function for EditFieldA
value = app.EditFieldA.Value
app.EditFieldB.Value = 2 * value;

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Community Treasure Hunt

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

Start Hunting!

Translated by