Accessing data in a struct retrieved from engGetVariable
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Kaustubha Govind
le 28 Avr 2011
If you have existing code that you can post, it might be useful for us to start with what you already have.
Réponses (2)
Chirag Gupta
le 28 Avr 2011
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 commentaires
James Tursa
le 28 Avr 2011
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.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!