Display a string output in a Edit Field Text box in App-Designer

조회 수: 149 (최근 30일)
Alex Smith
Alex Smith 2021년 4월 8일
댓글: Adam Danz 2021년 4월 9일
Hello, I have am developing a matlab app, where the app user inputs a number to a numeric edit field, and I need to output a string from a if statement seperating the different values. I am unable to get the text to display in the area?
function EntertheNumberofPeopleStayingEditFieldValueChanged(app, event)
People = app.EntertheNumberofPeopleStayingEditField.Value;
Liters = People * 150;
scatter(app.UIAxes,People,Liters)
end
% Value changed function: EditField
function EditFieldValueChanged(app, event)
if People > 4
text1 = sprintf('This is too many people for this shelter to accomodate.\n');
elseif People <= 0
text1 = sprintf('No water is needed.\n');
else
text1 = sprintf('Water needs are displayed here >>>\n');
end
app.EditField.Value = text1;
  댓글 수: 5
Alex Smith
Alex Smith 2021년 4월 8일
EditField is the output area. A number is entered into the EntertheNumberofPeopleStayingEditField. No error messages are displayed.
Adam Danz
Adam Danz 2021년 4월 9일
But I don't see where EditFieldValueChanged() is called.
If that function is supposed to be evoked by changes to EntertheNumberofPeopleStayingEditField, then shouldn't it be called from that function?

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

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 4월 8일
You have split your code between two different callback functions. However, only one will execute when a user inputs a value.
For your case, it is better if you put all the code you want to have happen when someone enters a number in the EntertheNumberofPeopleStayingEditFieldValueChanged callback function. Only this function will execute when the user changes this edit field's value.
In addition, keep in mind variable scope for a function. The variable People is only available inside the function that creates it.
function EntertheNumberofPeopleStayingEditFieldValueChanged(app, event)
People = app.EntertheNumberofPeopleStayingEditField.Value;
Liters = People * 150;
scatter(app.UIAxes,People,Liters)
if People > 4
text1 = sprintf('This is too many people for this shelter to accomodate.\n');
elseif People <= 0
text1 = sprintf('No water is needed.\n');
else
text1 = sprintf('Water needs are displayed here >>>\n');
end
app.EditField.Value = text1;
end

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by