1

I'm writing a function in an electrical engineering class as a part of a lab where we're to build an oscilloscope. This particular function is our software "trigger circuit." This function is just supposed to register as 1 if it meets certain conditions. The exact wording is:

"Write a MATLAB function called triggering_circuit that has the input parameters: past_sample, current_sample, trigger_level, and trigger_slope. The function is to return a value of 1 if trigger_level is between past_sample and current_sample and the difference between the current sample and the past sample (i.e., current_sample - past_sample) has the same sign as trigger_slope."

We feel as if we've written the function properly, but when we try to call it in our function, we get the error:

"Error in triggering_circuit (line 4) if trigger_level >= past_sample && trigger_level <= current_sample"

It's not giving any other errors, except that the function isn't assigning anything to the output variable m. I imagine, that's because the function won't finish running.

Now, I've looked online and I don't understand how we could be using the logical operator wrong. I would really appreciate any help.

The function is as follows:

function [ m ] = triggering_circuit( past_sample, current_sample, trigger_level, trigger_slope )

if trigger_level >= past_sample && trigger_level <= current_sample
    a = current_sample - past_sample;
    if a < 0 && trigger_slope < 0
        m = 1;
    elseif a > 0 && trigger_slope > 0
            m = 1;
    else
        m = 0;
    end
end
end
8
  • Verify your inputs are as expected, and I'd imagine you should set m=0 if you don't enter the main if-statement. You are also assuming that current_sample is bigger than past_sample. Commented Nov 13, 2013 at 23:32
  • Ok, so I added the m = 0, but that still doesn't fix the problem. Apparently my past_sample and current_sample were vectors, which I didn't originally realize. At that point I change the && to an & right? Is there anything else I have to change? Commented Nov 13, 2013 at 23:41
  • How can a vector be larger than an other vector? The description does not make sense for input vectors. Please provide some example input and describe the expected output. Commented Nov 13, 2013 at 23:43
  • Well it's difficult then, because then if some elements of one vector will be less than or equal to corresponding elements of the other vector, but other elements might not be. Then is your condition true or false? And then what do you do with those vectors to calculate m? Commented Nov 13, 2013 at 23:44
  • I apologize. Our main function is very large, and I misunderstood what was going on. past_sample and current_sample are just being changed in a for loop, they aren't actually vectors. An example of past_sample and current_sample: past_sample = 0.9884 current_sample = 1.0130 One last point, m is being set as 1 depending on the conditions. It's more of an indicator than anything else. sorry for the edits. Commented Nov 13, 2013 at 23:53

1 Answer 1

1
function [ m ] = triggering_circuit(past_sample, current_sample, trigger_level,  trigger_slope )
    if trigger_level >= past_sample && trigger_level <= current_sample
        a = current_sample - past_sample;
        if a < 0 && trigger_slope < 0
            m = 1;
        elseif a > 0 && trigger_slope > 0
            m = 1;
        else
            m = 0;
        end
    else
        m = 0; %# This is where you would set m = 0
    end
end

I am not sure if you have already figured it out, but you must return something for the output argument that is declared with the function (m in this case) and in the current setup, there is a case where nothing can be returned.

So a function call in your code such as this:

m = triggering_circuit(0.9884, 1.0130, 1, 1)
Returns m = 1 when you call it.

Also here are the references for the logical operands: http://www.mathworks.com/help/matlab/ref/logicaloperatorselementwise.html http://www.mathworks.com/help/matlab/ref/logicaloperatorsshortcircuit.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.