Effacer les filtres
Effacer les filtres

Function implementation matlab language

2 vues (au cours des 30 derniers jours)
Jimmy cho
Jimmy cho le 9 Août 2020
Modifié(e) : Stephen23 le 11 Août 2020
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 commentaires
Steven Lord
Steven Lord le 11 Août 2020
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 le 11 Août 2020
Modifié(e) : Stephen23 le 11 Août 2020
"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:

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 9 Août 2020
Modifié(e) : Walter Roberson le 11 Août 2020

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by