Complete Example for Passing Array by Reference to C for Processing

41 views (last 30 days)
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.

Accepted Answer

David Fink
David Fink on 22 Mar 2019
Thanks for the recommendation!
We're working on improving our documentation and this is valuable feedback.
Let's see if I can help clarify:
  1. "coder.varsize" is meant to be used within the MATLAB function for which you're generating code
  2. "coder.typeof" is meant to be used to create a type to pass to "codegen"
For your example, it sounds like you want the codegen argument (in args):
coder.typeof(uint8(42),[1 Inf],[0 1])
(This can be saved in a variable "t" like in your code.)
The [1 Inf] specify the maximum size in each dimension, and [0 1] indicate that the second dimension (and not the first) are variable-sized.
Based on the error (which comes from the C compiler), it seems like expects an unsigned character pointer as its second argument. However, it is being passed an int, and based on the MATLAB code, it should be passed a uint8.
crccalculate10.c is one of the C files generated by codegen, which is where the C compiler error occurs. Add "-report" (without quotes) as another argument to codegen, which displays the generated files and other information.
  3 Comments
Charles Campbell
Charles Campbell on 25 Mar 2019
Edited: Charles Campbell on 25 Mar 2019
This worked! That was the issue.
If anyone finds this and is looking for a concise example, see
  1. The Pass by Reference example here
  2. The typeof example provided in David's response here, with typeof docs here.
  3. Ensure that the ceval, prototype and definition of functions are all named the same.
  4. Ensure the matlab function is named something else.
With those considerations it should compile.
Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Code Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!