Function implementation matlab language

조회 수: 1 (최근 30일)
Jimmy cho
Jimmy cho 2020년 8월 9일
편집: Stephen23 2020년 8월 11일
Hi guys, Im coming with background of c/c++/java programming so I've done this function in c++:
uint16_t calc(uint8_t Data, uint16_t Reg)
{
uint8_t i;
unit16_t CRC16_POLY =0x8005;
for (i = 0; i < 8; i++)
{
if (((Reg & 0x8000) >> 8) ^ (Data & 0x80))
{
Reg = (Reg << 1) ^ CRC16_POLY;
}
else
{
Reg = (Reg << 1);
}
Data <<= 1;
}
return Reg;
}
So I want to do the same function but in matlab language, so what I've did is this(still not 100% work and there's a compilation error):
function uint16_t Reg= calc(uint8_t Data, uint16_t Reg)
uint8_t i;
CRC16_POLY =0x8005;
for i = 0:i < 8:i++
{
if ((bitshift((Reg bitwise 0x8000),8) ^ (Data bitwise 0x80)) %if this true then enter the if condition;
{ Reg = bitshift(Reg,1) ^ CRC16_POLY;}
else
{Reg = bitshift(Reg,1);}
bitshift(Data,1); %LEFT SHIFT
}
%once finished from loop for return the Reg variable ..
return Reg;
end
once I implemented that function in matlab I've a compilation error, could anyone please help me to implement properly that function as what I've done in c++? Yeah Im newbie in matlab and thanks alot for any assistance to implement correctly my function in matlab..
  댓글 수: 8
Steven Lord
Steven Lord 2020년 8월 11일
so what's wrong with my code now? it's matlab syntax ..
No, it's not.
function Reg= calc(Data,Reg)
This line is valid MATLAB syntax.
uint8_t i;
There's no function named uint8_t in MATLAB, and even if there was you probably don't want to call it with a char input.
CRC16_POLY =0x8005;
This works in sufficiently new MATLAB releases.
for i = 0:1:8
{
The body of a for loop is not wrapped in curly braces in MATLAB.
if ((bitshift((Reg bitwise 0x8000),8) ^ (Data bitwise 0x80)) %if this true then enter the if condition;
The "reg bitwise 0x8000" is going to throw an error and ^ is not the and operator.
I'm not going to look further in the code.
See the first page found by this search in the documentation:
docsearch bit-wise operations
Stephen23
Stephen23 2020년 8월 11일
편집: Stephen23 2020년 8월 11일
"so what's wrong with my code now? it's matlab syntax .."
No, you are still writing C++.
"if not, may please anyone give me an assistance to fix my code correctly in order to implement my function in matlab"
The best place to learn how to write MATLAB code are the introductory tutorials:

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 8월 9일
편집: Walter Roberson 2020년 8월 11일

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by