Selection of greater than or less than symbol in app

5 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by