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 Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by