Selection of greater than or less than symbol in app

5 Ansichten (letzte 30 Tage)
Jessica Hiscocks
Jessica Hiscocks am 24 Jan. 2018
Kommentiert: Jessica Hiscocks am 24 Jan. 2018
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

Akzeptierte Antwort

Adam
Adam am 24 Jan. 2018
Bearbeitet: Adam am 24 Jan. 2018
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 Kommentar
Jessica Hiscocks
Jessica Hiscocks am 24 Jan. 2018
Thank you, this is a whole new approach which I will keep in mind for future use!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by