Function implementation matlab language
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 9 Ago 2020
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
il 9 Ago 2020
Modificato: John D'Errico
il 9 Ago 2020
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
il 9 Ago 2020
the cyclist
il 9 Ago 2020
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
il 9 Ago 2020
Steven Lord
il 11 Ago 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
"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:
Risposte (1)
Walter Roberson
il 9 Ago 2020
Modificato: Walter Roberson
il 11 Ago 2020
0 voti
Categorie
Scopri di più su Startup and Shutdown in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!