0

I have converted a piece of code to C++ using Matlab and now have them in MSVC++.

My function: myFunction gets two inputs and has one output. Following, I tried to make the inputs, a, b, and allocate the output, but I got this error: error C3861: 'emxCreate_real_T': identifier not found

The function prototype looks like this, which in essence is C = A + B:

#include "myTestFunction.h"
#include "myTestFunction_emxutil.h"

void myTestFunction(const emxArray_real_T *A, const emxArray_real_T *B,
                    emxArray_real_T *C)
{
  int i0;
  int loop_ub;

  i0 = C->size[0] * C->size[1];
  C->size[0] = A->size[0];
  C->size[1] = A->size[1];
  emxEnsureCapacity((emxArray__common *)C, i0, (int)sizeof(double));
  loop_ub = A->size[0] * A->size[1];
  for (i0 = 0; i0 < loop_ub; i0++) {
    C->data[i0] = A->data[i0] + B->data[i0];
  }
}

and here is my main function:

int main() {
    double a[3][3];
    double b[2][2];
    double result[4][4] = {};

emxArray_real_T *inpA, *inpB, *outp;

// define input matrix
double p = 0;
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++){
        a[i][j] = p;
        p = p + 1;
    }
}

double k = 0;
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
        b[i][j] = k;
        k = k + 1;
    }
}

inpA = emxCreateWrapper_real_T(*a, 3, 3);
inpB = emxCreateWrapper_real_T(*b, 2, 2);
outp = emxCreateWrapper_real_T(*result, 4, 4);

//inpA = emxCreate_real_T(a, 3, 3);
//inpB = emxCreate_real_T(b, 2, 2);
//outp = emxCreate_real_T(result, 4, 4);

myTestFunction(inpA, inpB, outp);

//print result
for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++)
        cout << outp[i].data[j] << endl;
}

return 0;

}

How should I declare the inputs and output?

Sam
  • 939
  • 5
  • 14
  • 41
  • Where is the function `emxCreate_real_T` declared or included? The compiler can't find it... – JPhi1618 Oct 05 '15 at 18:23
  • I tried to follow [this](http://www.mathworks.com/matlabcentral/answers/85669-problem-using-emxarray_real_t-from-matlab-coder) link in mathworks. I think [emxCreate_real_T](http://www.mathworks.com/help/fixedpoint/ug/c-code-interface-for-unbounded-arrays-and-structure-fields.html) is a data-type and it is not a function. – Sam Oct 05 '15 at 19:16
  • Regardless of what it is, the compiler doesn't know about it. You must be forgetting an include file somewhere. Sorry to be so general, but I'm not that familiar with Matlab. Based on the second page you linked to, is there a `myTestFunction_emxAPI.h` that you can include? The linked page mentions `foo_emxAPI.h`... – JPhi1618 Oct 05 '15 at 19:27
  • Yes, I also have included the myTestFunction_emxAPI.h – Sam Oct 05 '15 at 19:34
  • I'm out of ideas. I made an edit to add the matlab tag to the question to try and attract a more knowledgeable crowd. – JPhi1618 Oct 05 '15 at 19:40
  • @JPhi1618: You were right! I had not included the headers! Sorry about that, however, I got this error now: – Sam Oct 05 '15 at 19:57
  • Loaded '...\Documents\Visual Studio 2010\Projects\testMatSimple\Debug\testMatSimple.exe', Symbols loaded. Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file 'testMatSimple.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded. Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted. The program '[11164] testMatSimple.exe: Native' has exited with code 0 (0x0). – Sam Oct 05 '15 at 19:58
  • Well, `b` is defined as `b[2][2]`, but in the loop you are using `3` for the set of for loops that access `b`. – JPhi1618 Oct 05 '15 at 20:00
  • (Posted as answer so it can be "accepted") – JPhi1618 Oct 05 '15 at 20:04

1 Answers1

0

You're missing an include file. Based on the link you provided in your comment, there is probably a myTestFunction_emxAPI.h file that needs to be included.

Also, I see that the b[2][2] array that you created is being accessed beyond its bounds by the for (int i = 0; i < 3; i++)' andfor (int j = 0; j < 3; j++)` loops.

JPhi1618
  • 783
  • 1
  • 11
  • 21
  • Thanks, I still get this error: 1>...\documents\matlab\codegen\lib\mytestfunction\mytestfunction.cpp(68): error C2676: binary '[' : 'emxArray_real_T' does not define this operator or a conversion to a type acceptable to the predefined operator 1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(153,5): error MSB6006: "CL.exe" exited with code 2. 1> 1>Build FAILED. – Sam Oct 05 '15 at 20:06
  • Yea, I think you've reached my limit of knowledge and are getting into Matlab only territory. I'm glad I could help with your original error or two, but looks like now you're going to have to find better samples of what you're trying to accomplish. – JPhi1618 Oct 05 '15 at 20:12
  • @Sam It's complaining about `cout << outp[i][j] << endl;` here, `outp[i]` doesn't have `operator []`. Try ``cout << outp[i].data[j] << endl;`` – user3528438 Oct 06 '15 at 17:03
  • @user3528438: Thanks, I get this error: 0xC0000005: Access violation reading location 0xfdfdfdfd, Am I using the right loop? – Sam Oct 06 '15 at 17:10
  • @Sam Maybe your `outp` only points one valid element. You need to revisit how you allocate for `outp`. – user3528438 Oct 06 '15 at 17:12