From: Doris wang on
I did the following code in C++,

%%%%
double a[10];
for (i=0; i<=10;i++)
a[i]=10;

mxArray *A *B;
A=mxCreateDoubleMatrix(1, 10, mxREAL);
memcpy((void *)mxGetPr(A), (void *)a, sizeof(a));
int flag=1;
engPutVariable(ep,"A",A);
flag=engEvalString(ep, "[B]=myfunction(A);");

%%%%%
[B]=myfunction(A) is some m-file function.
It seems that myfunction(A) is not evaluated although flag is 0 after execution. B is not even in the workspace.
Can "engEvalString" evaluate my function in m-files?
From: Jan Simon on
Dear Doris!

> double a[10];
> for (i=0; i<=10;i++)
> a[i]=10;
>
> mxArray *A *B;
> A=mxCreateDoubleMatrix(1, 10, mxREAL);
> memcpy((void *)mxGetPr(A), (void *)a, sizeof(a));
> int flag=1;
> engPutVariable(ep,"A",A);
> flag=engEvalString(ep, "[B]=myfunction(A);");

> [B]=myfunction(A) is some m-file function.
> It seems that myfunction(A) is not evaluated although flag is 0 after execution. B is not even in the workspace.
> Can "engEvalString" evaluate my function in m-files?

The description is at least incomplete.
1. Typo in "mxArray *A *B;", you mean "mxArray *A, *B;" ?!
2. Why is the flag 0 after execution? What does this exactly mean?
3. Did you define ep ? How?
4. You state, that B is is not "in the workspace" - in which workspace? How did you check this?
5. "It seems that myfunction(A) is not evaluated" - so does it just seem or is it really not evaluated? Please insert a trivial "disp('I'm running!')" in myfunction to clear this question.

There is no need to enclose a single expression in square brackets:
flag=engEvalString(ep, "B=myfunction(A);");

Please give us more details. Kind regards, Jan
From: Doris wang on
Thanks Jan, let me complete my question,
1. Yes, I meant mxArray *A, *B.
2. About the flag, the definition of "engEvalString" says it returns "0 if the command was evaluated by the MATLAB engine session, and a nonzero value if unsuccessful."
3. Here is the code about opening the engine and going to the folder where "myfunction" is.
Engine *ep;
//start MATLAB engine
if (!(ep = engOpen("\0"))) {
fprintf(stderr, "\n Can't start MATLAB engine\n");
return EXIT_FAILURE;
}
//folder where myfunction.m file is
engEvalString(ep, "cd /myfolder");

I did not include this in my last post is because the engine is running fine. I tried lines like "engEvalString(ep, "B=0.5*A;"), and they works fine.

4. There is a MATLAB window open by the codes in 3. I tried "who" in that session, I can only see variable A but not B. That's why I am saying that B is not in the workspace.

5. I am sure that "myfunction" is not evaluated, because I did add some thing like "disp ("I am running") in the .m file.

Hope I have cleared my question. Can anybody give me a hand here? Thanks.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hmimj8$6s$1(a)fred.mathworks.com>...
> Dear Doris!
>
> > double a[10];
> > for (i=0; i<=10;i++)
> > a[i]=10;
> >
> > mxArray *A *B;
> > A=mxCreateDoubleMatrix(1, 10, mxREAL);
> > memcpy((void *)mxGetPr(A), (void *)a, sizeof(a));
> > int flag=1;
> > engPutVariable(ep,"A",A);
> > flag=engEvalString(ep, "[B]=myfunction(A);");
>
> > [B]=myfunction(A) is some m-file function.
> > It seems that myfunction(A) is not evaluated although flag is 0 after execution. B is not even in the workspace.
> > Can "engEvalString" evaluate my function in m-files?
>
> The description is at least incomplete.
> 1. Typo in "mxArray *A *B;", you mean "mxArray *A, *B;" ?!
> 2. Why is the flag 0 after execution? What does this exactly mean?
> 3. Did you define ep ? How?
> 4. You state, that B is is not "in the workspace" - in which workspace? How did you check this?
> 5. "It seems that myfunction(A) is not evaluated" - so does it just seem or is it really not evaluated? Please insert a trivial "disp('I'm running!')" in myfunction to clear this question.
>
> There is no need to enclose a single expression in square brackets:
> flag=engEvalString(ep, "B=myfunction(A);");
>
> Please give us more details. Kind regards, Jan
From: James Tursa on
"Doris wang" <doris_wy_us(a)yahoo.com> wrote in message <hmjjso$48c$1(a)fred.mathworks.com>...
>
> //folder where myfunction.m file is
> engEvalString(ep, "cd /myfolder");

Did you check this result to see that the cd command worked? Maybe you could put in a engEvalString(ep, "C = cd"); statement and then get C to see what directory the engine is really in.

James Tursa
From: Doris wang on
I check in the open MATLAB window that the directory is "myfolder". Would that means the engine is really in "myfolder"?

Doris

"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hmjpcu$5r7$1(a)fred.mathworks.com>...
> "Doris wang" <doris_wy_us(a)yahoo.com> wrote in message <hmjjso$48c$1(a)fred.mathworks.com>...
> >
> > //folder where myfunction.m file is
> > engEvalString(ep, "cd /myfolder");
>
> Did you check this result to see that the cd command worked? Maybe you could put in a engEvalString(ep, "C = cd"); statement and then get C to see what directory the engine is really in.
>
> James Tursa