How do I write something to run this function?

I am relativitly new to MATLAB. Most of my new learnings have been learning how to write and run BASIC Functions. I downloaded this CRC32 bit function from File Exchange and I am having a hard time to run/callout the function.
I am trying to enter a HEX number and return the CRC Calculation in HEX.'
I am trying to calculate the CRC for something like "00060D00"
function crc = crc32(data)
%crc32 Computes the CRC-32 checksum value of a byte vector.
%--------------------------------------------------------------------------
% CRC = crc32(DATA) computes the CRC-32 checksum value of the data stored
% in vector DATA. The elements of DATA are interpreted as unsigned bytes
% (uint8). The result is an unsigned 32-bit integer (uint32). Polynomial
% bit positions have been reversed, and the algorithm modified, in order
% to improve performance.
% Version: 1.00
% Programmer: Costas Vlachos
% Date: 23-Dec-2014
% Initialize variables
crc = uint32(hex2dec('FFFFFFFF'));
poly = uint32(hex2dec('EDB88320'));
data = uint8(data);
% Compute CRC-32 value
for i = 1:length(data)
crc = bitxor(crc,uint32(data(i)));
for j = 1:8
mask = bitcmp(bitand(crc,uint32(1)));
if mask == intmax('uint32'), mask = 0; else mask = mask+1; end
crc = bitxor(bitshift(crc,-1),bitand(poly,mask));
end
end
crc = bitcmp(crc);
I saved it a new folder and path with nothing in there except this function (crc32.m)
When I click "Run" directly in the "Script" Tab/Window, this is what I get in the Command Window:
>> crc32
Not enough input arguments.
Error in crc32 (line 16)
data = uint8(data);
And takes me to the "Run" and shows this:
Can some explain this to me as well as how to call and use this function? Write a code? What I need to do to run and execute this funtion? How to enter?

2 Comments

I am trying to calculate the CRC for something like "00060D00"
When you run the function by name, what is your expectation of where matlab will search to figure out that it is 00060D00 that it is to operate on this run?
WARRIOR24
WARRIOR24 on 14 Dec 2020
Edited: WARRIOR24 on 14 Dec 2020
I believe:
>> data=[00060D00];

Sign in to comment.

 Accepted Answer

crc32(data);
%......^??
You have to pass the input arguments (data) as mentioned in the function code. e.g.
>>data=[1:18]; % Pass the appropriate data type as per function defined??
>>crc32(data)

8 Comments

WARRIOR24
WARRIOR24 on 14 Dec 2020
Edited: WARRIOR24 on 14 Dec 2020
I am assumping that [1:18] is where I enter my "00060D00" so it becomes
>>data=[00060D00];
>>crc32(data)
Although this the ans does not seem to be correct. I do not what is going on here
ans =
uint32
4251816714
I was expectiong something like: 1CB0B82F
The output is 32-bit data, not hexadecimal. It might also be that the function is incorrect as there is no guarantee that functions on the FEX do what they claim.
Thank you Rik you are the real MVP, I got it right.
Any recomendations on how to change it back to HEX within the code?
Thanks, Rik for your more clarification
dec2hex(ans)
We can use dec2hex() because we know that the result is uint32 and so cannot possibly exceed 2^53-1, the point at which double precision stops being able to represent integers accurately. This matters because dec2hex() converts to double precisio first.
WARRIOR24
WARRIOR24 on 14 Dec 2020
Edited: WARRIOR24 on 14 Dec 2020
Kalyan Acharjya, you are a GOD
How and where can I add the code turn it back into HEX? Writing anytype of code is really not my strong suit. I took JAVA like 9 years ago.
Thank you guys,
I have another question, later and it might along the lines of entering multiple inputs all at once:
"0000F0F03"
"AFCED902"
"['000F0990','00002020']"
Idk if that is possible to enter all at once. But the last input has me stopped now and if it is possible to enter that kind of entry.
As Walter mentioned: dec2hex will convert the value to hexadecimal without any issues.
dec2hex(crc32(data))
The function you showed doesn't handle array inputs by default, so you will have to use a loop to process your inputs one by one. It is also possible to use arrayfun to hide the loop.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Asked:

on 14 Dec 2020

Edited:

Rik
on 14 Dec 2020

Community Treasure Hunt

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

Start Hunting!