How to change code from C to matlab script?

1 view (last 30 days)
My code c is like this, how to change into matlab script?
float DNLRX (void)
{
float uf,x,r1,r2,r3,r4,r5,r6,s1,s2,s3,s4,s5,s6,s7;
float k1,k2,k3,k4,k5,k6,k7,z1,z2,z3,z4,z5,z6,z7,l1,l2,l3,l4,l5,l6,l7;
float B=0.0026;
r1=6.4527;r2=-4.6055;r3=-1.84030;r4=-1.8386;r5=-4.5868;r6=6.4215;
s1=1.0439;s2=14.3983;s3=0.0001;s4=-0.1792;s5=-14.3811;s6=-0.8453;s7=0.0018;
k1=SIGN(a-b+d1);
z1=abs(a-b+d1);
l1=Min(z1,0.0708);
d1=k1*l1;
k2=SIGN(a-b+d2);
z2=abs(a-b+d2);
l2=Min(z2,0.0737);
d2=k2*l2;
k3=SIGN(a-b+d3);
z3=abs(a-b+d3);
l3=Min(z3,54.1993);
d3=k3*l3;
k4=SIGN(a-b+d4);
z4=abs(a-b+d4);
l4=Min(z4,0.0353);
d4=k4*l4;
k5=SIGN(a-b+d5);
z5=abs(a-b+d5);
l5=Min(z5,0.0734);
d5=k5*l5;
k6=SIGN(a-b+d6);
z6=abs(a-b+d6);
l6=Min(z6,0.0779);
d6=k6*l6;
k7=SIGN(a-b+d7);
z7=abs(a-b+d7);
l7=Min(z7,1.0964);
d7=k7*l7;
x=s1*d1+s2*d2+s3*d3+s4*d4+s5*d5+s6*d6+s7*d7; //theta*delta
uf=r1*a+r2*b+r3*c+r4*d+r5*e+r6*f+x+B; //maxwell equation
f=e;
e=d;
d=c;
c=b;
b=a;
a=z; //z=new_postion
return uf;
}

Accepted Answer

James Tursa
James Tursa on 8 Jun 2020
Edited: James Tursa on 8 Jun 2020
This is just straightforward arithmetic, so the conversion is pretty simple. I would forget about using single precision floats though unless you are trying to match a specific precision calculation. Just use doubles.
However, it appears that some of the inputs are top level variables that are not coming into the function via the argument list which is void. So you will have to figure out what those variables are and put them into the input argument list. And it appears that these top level variables are also outputs of the function, so they will need to be in the output argument list as well. E.g.,
E.g.,
% Put this in a file called DNLRX.m
function [uf,a,b,d1,...etc] = DNLRX(a,b,d1,...etc) % you fill this in for all the I/O variables
SIGN = @sign;
Min = @min;
B=0.0026;
r1=6.4527;r2=-4.6055;r3=-1.84030;r4=-1.8386;r5=-4.5868;r6=6.4215;
s1=1.0439;s2=14.3983;s3=0.0001;s4=-0.1792;s5=-14.3811;s6=-0.8453;s7=0.0018;
k1=SIGN(a-b+d1);
z1=abs(a-b+d1);
l1=Min(z1,0.0708);
d1=k1*l1;
k2=SIGN(a-b+d2);
z2=abs(a-b+d2);
l2=Min(z2,0.0737);
d2=k2*l2;
k3=SIGN(a-b+d3);
z3=abs(a-b+d3);
l3=Min(z3,54.1993);
d3=k3*l3;
k4=SIGN(a-b+d4);
z4=abs(a-b+d4);
l4=Min(z4,0.0353);
d4=k4*l4;
k5=SIGN(a-b+d5);
z5=abs(a-b+d5);
l5=Min(z5,0.0734);
d5=k5*l5;
k6=SIGN(a-b+d6);
z6=abs(a-b+d6);
l6=Min(z6,0.0779);
d6=k6*l6;
k7=SIGN(a-b+d7);
z7=abs(a-b+d7);
l7=Min(z7,1.0964);
d7=k7*l7;
x=s1*d1+s2*d2+s3*d3+s4*d4+s5*d5+s6*d6+s7*d7; //theta*delta
uf=r1*a+r2*b+r3*c+r4*d+r5*e+r6*f+x+B; //maxwell equation
f=e;
e=d;
d=c;
c=b;
b=a;
a=z; //z=new_postion
end

More Answers (1)

Abhisek Pradhan
Abhisek Pradhan on 8 Jun 2020
There is no direct way of converting C code to MATLAB Script as of now. But you can use MEX Files to run C Code in MATLAB Environment. Refer the following link:

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!