필터 지우기
필터 지우기

Selection of greater than or less than symbol in app

조회 수: 3 (최근 30일)
Jessica Hiscocks
Jessica Hiscocks 2018년 1월 24일
댓글: Jessica Hiscocks 2018년 1월 24일
As part of my app, I have three fields; the first selects the parameter to filter on (from part of a structure), the second picks between >,<,= symbol, and the third contains a numeric value.
This allows me to filter the data (TempGrainsData) by the parameter (e.g. area> 100). I have programmed the app to evaluate the input using a series of nested if statements (see code below), which works. However, if the user has 2 parameters to choose from, and there are three options for comparison (>,<,=) this results in 6 if statements. This method will very rapidly become unwieldy if I want more parameters. Looking at the code below, is there a better way to program this?
if app.ListBox.Value==1
if app.DropDown.Value==1
test=app.TempGrainsData(app.TempGrainsData.area>app.EditField.Value);
elseif app.DropDown.Value==2
test=app.TempGrainsData(app.TempGrainsData.area<app.EditField.Value);
else
test=app.TempGrainsData(app.TempGrainsData.area==app.EditField.Value);
end
else
if app.DropDown.Value==1
test=app.TempGrainsData(app.TempGrainsData.aspectRatio>app.EditField.Value);
elseif app.DropDown.Value==2
test=app.TempGrainsData(app.TempGrainsData.aspectRatio<app.EditField.Value);
else
test=app.TempGrainsData(app.TempGrainsData.aspectRatio==app.EditField.Value);
end
end

채택된 답변

Adam
Adam 2018년 1월 24일
편집: Adam 2018년 1월 24일
I would just use a single if statement to get your operand as a string and convert it to a function handle, e.g
func = @gt;
func = @lt;
func = @eq;
would be the function handles for your given operands which can be used as e.g.
>> func = @gt;
func( 8, 7 )
ans =
logical
1
>> func( 7, 8 )
ans =
logical
0
Then just get the two operands from their respective controls and pass them to your function handle, which doesn't need to know which of the operators it is any more.
doc gt;
doc lt;
doc eq;
will give more information on these. It is useful to be aware of the named function equivalents of operators such as these. Every operator that uses a symbol also has a named function equivalent.
  댓글 수: 1
Jessica Hiscocks
Jessica Hiscocks 2018년 1월 24일
Thank you, this is a whole new approach which I will keep in mind for future use!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by