MATLAB Answers


Building a switch so allow&forbid a signal

Asked by Lucas
on 19 Jan 2013

Hey! I would like to create a kind of switch that allows the exsistence of a signal under specific circumstances in MatLab. There are about 91.000 checkpoints in which the switch has to operate to give an answer to the question: does contact exsist? It should say either "yes" (1) or "no" (0) for each of these 91.000 checkpoints.

There is the position of a tool which is declared to be Xsollf(i),Ysollf(i),Zsollf(i) which is variable in time and the position data of a work piece which reaches from X1 to X2, Y1 to Y2 and Z1 to Z2 which is fixed.

So now i tried something like this:

cuttingforce(i) = zeros(1,length(Tsoll)); % Tsoll is the vector of time

for i = 2:length(Tsoll)

    if Z1<Zsollf(i) && Zsollf(i) <Z2 && Y1<Ysollf(i) &&  Ysollf(i) <Y2 && X1<Xsollf(i) && Xsollf(i)<X2
    end % of if
end %of for

I wanted to use &&-operator that saves a little bit of time by stopping the examination whenever one of these if conditions is flase. Each time the if conditions are true which means the tool is on the surface of the work piece or within its body the element of the cutting force vector should be changed from 0 to 1 so that there can exsist cutting force.

I am just an amateur and by now I just can not find a solution. I would be grateful to read your ideas.


Heureka! Found the answer myself!

cuttingforce = [];
for i = 2:length(Tsoll)      
    if (Z1 < Zsollf(i) && Zsollf(i) < Z2 &&...
        Y1 < Ysollf(i) && Ysollf(i) < Y2 &&...
        X1 < Xsollf(i) && Xsollf(i) < X2)  
        Fzerspan(i) = 1;
        cuttingforce(i) = 0

The pre-allocation is important for efficient code:

cuttingforce = zeros(1, length(Tsoll));

Not cuttingforce(i).

What is Fzerspan? And in your latest comment, cuttingforce = 0 if the condition is met but at the start you set it to 1. So I wasn't quite sure what to do in my vectorized answer below.

Log in to comment.

1 Answer

Answer by Image Analyst
on 19 Jan 2013

Try this vectorized way:

cuttingforce = zeros(1, length(Tsoll));
Fzerspan = zeros(1, length(Tsoll))
inRange = Z1 < Zsollf(i) && Zsollf(i) < Z2 &&...
          Y1 < Ysollf(i) && Ysollf(i) < Y2 &&...
          X1 < Xsollf(i) && Xsollf(i) < X2)
% If in range, set Fzerspan and cuttingforce.
Fzerspan(inRange) = 1;
cuttingforce(inRange) = 1;


Log in to comment.

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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!