Changing variable value based off x/y location of a product

2 views (last 30 days)
I've written some code to monitor a mock retail outlet which loads product location (x/y) data from a .xls file and plots it on top the stores blueprint.
the blueprint has defined payment and exit areas, and each product has a payment status variable (P#pay) where 0 = unpaid and 1 = paid. If the product enters the payment area the paystatus variable changes from 0 to 1. I wrote a for/if statement to handle this operation but it's not working properly and I cant seem to figure out why. Below is the code, and thanks for taking the time to look this over!
% Blank, Jacob
clear all
close all
clc
%define store dimensions blueprint
hold on
rectangle('position',[0,0,120,110])
%define shelving units
rectangle('position',[15,20,10,75],'facecolor','y')
rectangle('position',[35,20,10,75],'facecolor','y')
rectangle('position',[55,20,10,75],'facecolor','y')
rectangle('position',[75,10,10,30],'facecolor','y')
rectangle('position',[90,10,10,30],'facecolor','y')
rectangle('position',[105,10,10,30],'facecolor','y')
%define freezer space
rectangle('position',[75,75,40,15],'facecolor','c')
rectangle('position',[75,50,40,15],'facecolor','c')
%define payment portal
rectangle('position',[15,0,20,10],'facecolor','g')
text(21,5,'PAY')
payYlimMax=10;
payYlimMin=0;
payXlimMax=35;
payXlimMin=15;
%define exit
rectangle('position',[0,0,15,10],'facecolor','r')
text(2,5,'EXT')
extYlimMax=10;
extYlimMin=0;
extXlimMax=15;
extXlimMin=0;
axis on
%product 1 location data
P1 = xlsread('location_data','Product 1');
n=size(P1,1);
P1X=P1(2:n,1);
P1Y=P1(2:n,2);
plot(P1X,P1Y,'ko');
%product 1 payment status
P1pay=0;
for i=1:n-1
if payXlimMin < P1X(i) < payXlimMax && payYlimMin < P1Y(i) < payYlimMax
P1pay = 1;
end
end
%product 2 location data
P2 = xlsread('location_data','Product 2');
n2=size(P2,1);
P2X=P2(2:n2,1);
P2Y=P2(2:n2,2);
plot(P2X,P2Y,'ko');
%product 2 payment status
P2pay=0;
for i2=1:n2-1
if payXlimMin < P2X(i2) < payXlimMax && payYlimMin < P2Y(i2) < payYlimMax
P2pay = 1
end
end
hold off
  1 Comment
Jacob
Jacob on 8 Mar 2012
if it helps, you can download location_data.xls and the .m file from my box:
http://www.hcusa.net/matlab/location_data.xls
http://www.hcusa.net/matlab/Blank_Jacob_proj_1.m
thanks again!

Sign in to comment.

Accepted Answer

Jacob
Jacob on 8 Mar 2012
" When you write "a<b<c" as you have, it can give you very unexpected results. Very likely you want it to mean "a is less than b and b is less than c", but that is not how matlab interprets it. It groups things this way: (a<b)<c and first determines whether a<b is true or false. Then since (a<b) looks to matlab as though it were a numerical quantity because it occurs in another inequality, it converts the true/false state to a 1 or 0, respectively, and determines whether that 1 or 0 is numerically less than c. This is very far from what you undoubtedly had in mind.
Always write (a<b)&(b<c) and never a<b<c to avoid catastrophes like this.
Roger Stafford "

More Answers (1)

Walter Roberson
Walter Roberson on 8 Mar 2012

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!