MATLAB: Hi everyone, I am trying to write the below C code in MATLAB but am not able to get the counter running. Can anyone help me out please

counter

static double counter=0;
static double counter2;
static double theta_D;
static int flag;
counter=0;
counter2=400;
flag=0;
double Freq;
theta_D = InputSignal(0,0);
if(IsSampleHit(1))
{
counter++;
}
if(theta_D>=359)
{
if(flag==0)
{
counter2 = counter;
flag = 1;
}
counter=0;
}
else
{
flag = 0;
}
Freq = 20000/counter2;
OutputSignal(0, 0) = Freq;
OutputSignal(0, 1) = counter;

Best Answer

function [Freq, OutputSignal] = cntr(InputSignal, IsSampleHit)
bug_for_bug_compatible = true;
if bug_for_bug_compatible
arg_offset = 0;
else
arg_offset = 1;
end
assert( exist('InputSignal', 'var') );
assert( exist('IsSampleHit', 'var') );
persistent counter counter2 theta_D flag
if isempty(counter)
counter = 0;
counter2 = 400;
flag = 0;
if bug_for_bug_compatible
assert( isa(InputSignal), 'function_handle') );
end
theta_D = InputSignal(0 + arg_offset, 0 + arg_offset);
end
if bug_for_bug_compatible
counter = 0;
counter2 = 400;
flag = 0;
end
if bug_for_bug_compatible
assert( isa(IsSampeHit), 'function_handle') );
end
if isSampleHit(1 + arg_offset)
counter = counter + 1;
end
if theta_D >= 359
if flag == 0
flag = 1;
counter2 = counter;
end
counter = 0;
else
flag = 0;
end
Freq = 20000/counter2;
if bug_for_bug_compatible
error('The value of a function call is never an lvalue in C');
else
OutputSignal(0 + idx_offset, 0 + arg_offset) = Freq;
OutputSignal(0 + idx_offset, 1 + arg_offset) = counter;
end
end