Reputation: 41
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
Upvotes: 1
Views: 376
Reputation: 134
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
Upvotes: 1