フィルターのクリア

Accessing data in a struct retrieved from engGetVariable

2 ビュー (過去 30 日間)
Sean McGhee
Sean McGhee 2011 年 4 月 28 日
I am pulling hen's teeth trying to actually access the data - I started a new question thread as the old one might be closed due to my accepting the answer(which was quite helpful - no problems there)...
I cannot seem to get data out - i need to see and end to end example of:
1-getting the struct using engGetVariable (already know this part) 2-accessing the desired field (i THINK this works but cannot be sure) 3-actually retrieving the data in the selected field (presumably using mxGetData and some mxCopyTo...routine)
I will look in the other examples in the externals directory but I am doubtful i will find a simple start to finish example of this
Thanks! Sean
  2 件のコメント
Sean McGhee
Sean McGhee 2011 年 4 月 28 日
I am in the C realm, not FORTRAN if that helps
Kaustubha Govind
Kaustubha Govind 2011 年 4 月 28 日
If you have existing code that you can post, it might be useful for us to start with what you already have.

サインインしてコメントする。

回答 (2 件)

Chirag Gupta
Chirag Gupta 2011 年 4 月 28 日
Hi Sean,
Here's my trivial example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define BUFSIZE 256
int main()
{
Engine *ep;
mxArray *result = NULL;
char buffer[BUFSIZE+1];
if (!(ep = engOpen("\0"))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
/* Create a dummy structure in Matlab */
engEvalString(ep, "mystruc.A ='helloworld'");
engEvalString(ep, "mystruc.B = 45.6");
/* Retrieve the structure */
result = engGetVariable(ep,"mystruc");
if(mxIsStruct(result))
{
printf("As expected a structure\n");
printf("Accessing structure:\n");
mxGetString(mxGetField(result,0,"A"), buffer, BUFSIZE);
printf("Field A: %s\n",buffer);
printf("Field B: %g\n",mxGetScalar(mxGetField(result,0,"B")));
}
else
{
printf("Oops\n");
mxDestroyArray(result);
engClose(ep);
return EXIT_SUCCESS;
}
printf("Hit return to continue\n\n");
fgetc(stdin);
printf("Done!\n");
mxDestroyArray(result);
engClose(ep);
return EXIT_SUCCESS;
}
And its output:
c:\MatlabWork\Answers>engdemo As expected a structure Accessing structure: Field A: helloworld Field B: 45.6 Hit return to continue
Done!
c:\MatlabWork\Answers>
  2 件のコメント
Sean McGhee
Sean McGhee 2011 年 4 月 28 日
Much appreciates, Chirag!
It sadly doesn't work for me and now I am more convinced that I am doing something wrong...:-(
One question: What is the purpose of the index variable in mxGetField (parameter 2)...
James Tursa
James Tursa 2011 年 4 月 28 日
For the 0 index in the mxGetField function, you are doing the equivalent of mystruc(1).A. If you used a 1 index you would be doing the equivalent of mystruc(2).A, etc. I would also point out in the above example that you don't *have* to close the engine when the program ends. This can be useful e.g. if you have some figures up on the screen and you want them to stay there after your program ends. You can always manually close the engine later. Also it is a good idea to check that the return value of engGetVariable is not NULL before using it. As far as your engine code not working, can you be more explicit? Does the program compile and run but you get the "Can't start MATLAB engine" message? If so, maybe you need to register MATLAB as a server. On Windows I think you just bring up a Command Line window and enter MATLAB /regserver at the prompt.

サインインしてコメントする。


Sean McGhee
Sean McGhee 2011 年 4 月 28 日
OK - I will have to send MATLAB an email officially requesting assistance on this one. I have done everything everyone has suggested and it looks completely reasonable and I tried it on a trivial case of a single array of 4 elements and it worked fine.
When I run code which returns a struct with multiple fields of varying types it acknowledges the existence of the struct. I then ask it to get me a field and it complies without complaint but then the calls to extract the value of that field, which i can see in the matlab command window, the values are wrong. not even close.
I can, weirdly enough, access and collect the names of the fields but nothing else. I am calling matlab code via the matlab engine using engOpen, engEvaluateString, engGetVariable, mxGetField, mxGetPr, Mx...yadayadayada...and...
Nada.
This is ridiculously frustrating and I am beginning to understand how the main character of "Powder" felt everytime he tried to watch TV (a deep reference...I am a movie buff)...
So, again, thanks for all the effort and help!

Community Treasure Hunt

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

Start Hunting!

Translated by