Function implementation matlab language
이전 댓글 표시
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
Image Analyst
2020년 8월 9일
There is a whole family of functions for bitwise operations. Look in the help for function called like bit*****(). For example bitshift(), etc.
John D'Errico
2020년 8월 9일
편집: John D'Errico
2020년 8월 9일
But essenitally nothing of what you wrote that you want to be in MATLAB, is actually legal syntax for MATLAB.
Ok, the end statement is that.
It seems logical that one would learn the syntax of a language, if you want to write code in said language.
Jimmy cho
2020년 8월 9일
the cyclist
2020년 8월 9일
As John stated, you are simply not coding in MATLAB syntax.
For example, curly brackets mean something completely different in MATLAB (demarcating cell array) and are not used in for loops.
I'm not sure why you think the conversion from C++ to MATLAB would be so straightforward, but it just isn't. You need to learn at least the basics of a language to program in it. This purpose of this forum is not really to do that.
Jimmy cho
2020년 8월 9일
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
"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
2020년 8월 9일
편집: Walter Roberson
2020년 8월 11일
0 개 추천
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!