MATLAB: Adding more than one greater/less than condition to an ‘if statement’

conditionalgreater thanlatitudeless thanlongitudepoint inside rectangle

Simply trying to construct an if statement asking whether a longitude/latitude coordinate for an image is located within a rectangular section.
for i = 1:number_of_images
if (im_lat(i) > lat_D && im_lat(i) < lat_A) && (im_lon(i) > lon_A && im_lon(i) < lon_B)
...
end
end
Where A is the top left corner of the rectangle, B is the top right, and D is the bottom left. I'm curious if there is an easy way of revising this statement. There may also be functions that do this, such as inpolygon; however, I'm not sure how to integrate anything like this with my current format. I greatly appreciate any information you are able to provide.

Best Answer

inpolygon can do it but you've then got to return both output variables and do the set difference between in and on to get those inside as it returns inclusive of boundary and your test is written as exclusive. In addition, it's more verbose to define the vertices of the square than write the explicit test value.
I use a little helper function that is simply "syntactic sugar" to simplify such that lets you write the above as
if iswithinex(im_lat(i),lat_D,lat_A) & iswithinex(im_lon(i),lon_A,lon_B)
where iswithinex is
>> type iswithinex
function flg=iswithinex(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithinex(x,lo,hi)
% returns T for x between lo and hi values, exclusive
flg= (x>lo) & (x<hi);
>>
Originally I began with iswithin(x,lo,hi,flg) where the flag was for [in|ex]clusive but couldn't ever remember which way had defined it so I ended up with two specific versions. I suppose probably the way would have been to have made the flag input the string instead of a logical; in fact, I may try that variation and see how I like it... :)