Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
For/If statement monitoring position of item

Subject: For/If statement monitoring position of item

From: Jacob

Date: 8 Mar, 2012 02:06:12

Message: 1 of 4

I'm trying to create a demo of a store monitoring system which takes location data, maps it over a store blueprint with defined exit and payment areas, and displays a warning if a product enters the exit area without first entering the payment area.

with the code i've written so far, everything works fine except for changing the payment status of a product (variable P#pay) from unpaid (0) to paid (1). The code i wrote changes the payment status to 1 regardless of location, and I'm not sure what i did wrong. [note: i've yet to code the pay/nopay warning as i'm stuck here....] below is the code, and thanks for your help!

% 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;
%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

Subject: For/If statement monitoring position of item

From: Jacob

Date: 8 Mar, 2012 02:35:12

Message: 2 of 4

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!

Subject: For/If statement monitoring position of item

From: Roger Stafford

Date: 8 Mar, 2012 03:22:17

Message: 3 of 4

"Jacob " <jblank@tulane.edu.removethis> wrote in message <jj946k$954$1@newscl01ah.mathworks.com>...
> .....
> ..... The code i wrote changes the payment status to 1 regardless of location, and I'm not sure what i did wrong. .....
> .....
> if payXlimMin < P1X(i) < payXlimMax && payYlimMin < P1Y(i) < payYlimMax
> .....
- - - - - - - -
  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

Subject: For/If statement monitoring position of item

From: Jacob

Date: 8 Mar, 2012 03:36:26

Message: 4 of 4

Worked like a charm! thanks so much :D

"Roger Stafford" wrote in message <jj98l9$lgm$1@newscl01ah.mathworks.com>...
> "Jacob " <jblank@tulane.edu.removethis> wrote in message <jj946k$954$1@newscl01ah.mathworks.com>...
> > .....
> > ..... The code i wrote changes the payment status to 1 regardless of location, and I'm not sure what i did wrong. .....
> > .....
> > if payXlimMin < P1X(i) < payXlimMax && payYlimMin < P1Y(i) < payYlimMax
> > .....
> - - - - - - - -
> 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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us