필터 지우기
필터 지우기

Matlab App - Value of numeric field to reset to default starting value if condition not met

조회 수: 14 (최근 30일)
Hi folks,
I am creating an app in which I need to specify some numeric values, among which:
  • a start distance
  • an end distance
  • a step
This in turn creates a distance vector.
I then have 3 numeric fields that the user can edit with the desired distance.
I set a "default" starting value, which shows up in the boxes when the app starts.
I want to implement condition tests whenever the user tries to edit those fields. If the input start distance is higher than the end distance, it should display a warning message box and I would like the start distance to reset automatically to the value it was at. The problem is that the default starting value is stored as not stored as app.numericfield.DefaultValue but as app.numericfield.Value. So the default value is erased each time.
Has anyone got a solution for that?
Thanks in advance

채택된 답변

Mario Malic
Mario Malic 2021년 4월 29일
Hi Antoine,
Assuming you have a simpler app, you can create a property that will hold your default values, for one or more components.
properties (Access = Public)
defValues = struct();
end
Then create a startupFcn callback (read about it), you can define initial values for some components from which you'll read default values
function startupFcn(app)
defValues.Component1 = 5;
defValues.Component2 = 2;
end
Then, in callback which changes the component value, you can set the default one in case condition is not fulfilled
function EditFieldValueChangedFcn(app, event)
if %condition
% do something
else
app.EditField.Value = app.defValues.Component1;
% uialert('title', 'text'); use uialert to display alerts/warnings
end
end
  댓글 수: 1
Antoine Purier
Antoine Purier 2021년 5월 3일
Hi Mario,
Thaks for taking the time to provide such a complete answer.
It works perfectly after implementation.
I actually implemented something slightly different as I stored the previous value and not the default starting value. So the value that is in the text field just before entering a new "wrong" one.
But the logic is here!
Many thanks
Antoine

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by