0

I have been coding in MATLAB and I managed to convert my work to a single function, however it has several inputs and outputs. For the sake of simplicity, lets say it receives three inputs: X (vectorial and for reading only), Y (vectorial and for reading and writing) and Z (scalar and for writing only). Thanks to the reply here I was able to understand that I must create variables with special MATLAB types in order to pre-allocate space and then send them as parameters in my function in the C code.

An initial version with a single scalar output (Z) worked as expected, however taking the next step towards having multiple outputs has raised some questions. I'll try to be as concise as possible. Here's the header of my function in MATLAB and C code once I change Z to a vector:

[Y,Z]=foo(X,Y)

void foo(const unsigned int *X, float Y[n_Y], float Z[n_Z])

These are my doubts so far.

1 - I would expect that if Z is only created inside, it should not appear as an input for the C function. What should I do with it in order to obtain it outside the function? My idea would be to provide a fake variable with the same name that would later be overwritten.

2 - If Y is being changed, then the function should receive the pointer to Y. Is it being updated this way, as it should?

3 - Right now the dimensions are set for X as (1x:inf), which causes the pointer to show up. If I change to a smaller and realistic bound, that single input transforms into two, although nothing else changed (the variable creation in C is independent). Now there is const unsigned int X_data[], const int X_size[2] instead of just const unsigned int X. How should I deal with it within the C code?

The call to the function in C is being made as follows:

emxArray_uint32_T *X=emxCreate_uint32_T(1,n_X);
static emxArray_uint32_T *Y=emxCreate_real32_T(1,n_Y), *Z=emxCreate_real32_T(1,n_Z);

foo(X,&Y,&Z);

emxDestroyArray_uint32_T(X);

I should say that I have not tried to compile the lastest steps, since I need a specific environment to do so (laboratory). However, when I have access to it, the code needs to be almost ready to go. Also, without solving these doubts I think I shouldn't anyway. If it works somehow and I don't understand why, then it's the same as not working.

Community
  • 1
  • 1
Alegom
  • 105
  • 1
  • 2
  • 10
  • If `Y` and `Z` are outputs, they should be passed by pointer. The signature for `foo` in your first code block doesn't match the call in the second code block. Can you check that they are correct and copy and paste the function declarations from the MATLAB code and the generated C code? Also, the [documentation](http://www.mathworks.com/help/coder/ug/calling-generated-c-c-functions.html) describes the calling convention for the generated code. – Ryan Livingston Oct 06 '15 at 14:15
  • Hi @RyanLivingston, I was hoping you'd show up! I've seen you providing valuable assistance in Coder related issues, I just couldn't contact you directly. Yes, they should be passed by pointer. That ends up being the idea I proposed, but using the proper names. It has been some time since I coded in C. The second block has my call to the function, now I understand I must create Z and pass it as input to `foo` for it to be changed inside. What confuses me right now is the lack of `*` in Y and Z at the header of foo as generated by Coder and how changing dimensions for X causes a `*` to show up. – Alegom Oct 07 '15 at 15:31
  • Glad some of my posts have been helpful! My point was that the declaration of `foo` in the first block doesn't match the way you're calling it in the second block (for starters there's only 2 arguments in the second but 3 in the first). We need more information to help. Can you copy and paste the MATLAB Code, the header's declaration, and include how you called `codegen`? – Ryan Livingston Oct 07 '15 at 15:51
  • There is also a [demo](http://www.mathworks.com/help/coder/ug/generate-and-modify-an-example-cc-main-function.html) that uses an [example main file](http://www.mathworks.com/help/coder/ug/generate-an-example-cc-main-function.html) to show how to call the generated code. Does that help? – Ryan Livingston Oct 07 '15 at 15:52
  • Yes, I agreed with you in my last comment, but I guess I didn't update the OP. Well, except for the names of the variables in particular, these are the same as my MATLAB code and the generated header. Regarding the codegen call, I launched coder and inserted the variable's sizes at the Entry-Point Files of the interface. There is where I set `X` as a `uint32(1x:inf)`, but by setting an upper bound as `uint32(1x:1000)` the size variable appears in the header generated as mentioned. I guess I should pass the size explicitly if I chose to set such a bound. I hope it works later at testing. – Alegom Oct 08 '15 at 15:53
  • Hum... Let's say I cannot exactly access that online documentation in particular, if you know what I mean... – Alegom Oct 08 '15 at 15:53

0 Answers0