I am having issues getting Matlab Coder to allow me to pass an array into a CRC calculator .m file, then pass that array down to C and have C do the processing.
function crc = crccalculate10(in1) %#codegen
assert(isa(in1,'uint8')); assert(all(size(in1) <= [inf])); crc = 0; % generate an include in the C code
coder.cinclude('crccalculate10.h'); % evaluate the C function
crc = coder.ceval('crccalculate10', coder.rref(in1), uint8(numel(in1)));end
As far as I can tell, the issue isn't in the code, it's in the codegen params, especially the -args <> argument, where you would normally specify what variables can be passed in. But I cannot find any examples of passing, what I understand needs to be a variable length array. It is documented in several places, for example here, which lacks the codegen command itself to specify the length. And here provides some examples of fixed size matrices being passed in, but no variable sized. And if you read the coder.varsize documentation, every example shows varsize operating on the output variable, never the input variable. This guy seems to have similar questions on the use of coder.varsize and coder.typeof.
My best guess on use use of typeof basically involves building a coder.Type variable and passing that. The procedure would be basically,
x = [uint8(1) uint8(1) uint8(1)]coder.varsize('x', [1, Inf])t = coder.typeof(x)codegen crccalculate10 -args {t} crccalculate10.c
I checked the type and it looks correct to me. But also, I tried using coder.varsize(t, [1 Inf]). Both cases lead to a compiler error, on a line in my .c that doesn't exist (?!),
Error crccalculate10.c: 19 type error in argument 2 to `crccalculate10'; found 'int' expected 'pointer to unsigned char'
Unfortunately I don't have a line 19 in either my C source or my M file, so that compiler output is very incorrect. But, if I had to guess, the value of coder.rref(in1) is not "t" specified above.
At this point I have exhausted basically everything I can think of to make this work. I might just have to try to reimplement it in Matlab, it might have been faster. Any assistance would be appreciated. That said, I think this use case, being incredibly common in C code, deserves a clear and concise example in the documentation.
Best Answer