print mxArray type.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mohammed Samdani
am 27 Jan. 2012
Kommentiert: Gauraang Khurana
am 2 Jan. 2016
Hi,
I want to know how to print mxArray data type. I have a program below
int main(int ac, const char *av[]) {
int param1 = atoi(av[1]); int param2 = atoi(av[2]);
mxArray *in1 = NULL, *in2= NULL;
printf(" input param1 = %d\n", param1); printf(" input param2 = %d\n", param2);
in1 = mxCreateDoubleScalar(param1); in2 = mxCreateDoubleScalar(param2);
}
I want to know how to print in1 & in2
I also want to know how to print mxArray type, if mxArray was string , array , matrix or struct.
If a I have to use any function for this which header file should I include in my .c file
Thansk in advance
0 Kommentare
Akzeptierte Antwort
Friedrich
am 30 Jan. 2012
Okay, ML Compiler DLL. There is no print function available in C for the mxArray data type. One thing I use quite often in such a case is that I add a specific function to my generated DLL:
function chararray = mlprint(input)
chararray = evalc('disp(input)');
end
In that way you use the MATLAB display functionality, capture this in a char array and return it back to the C side. Once you have that char array in C, you can apply the normal printf on it.
I created a DLL with the name printtest which contains the function above. On the C side I do:
#include <stdio.h>
#include "printtest.h"
int main()
{
mxArray *test;
mxArray *out = NULL;
double data[] = {1,2,3,4,5,6,7,8,9};
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
return -1;
}
test = mxCreateDoubleMatrix(3,3,mxREAL);
memcpy(mxGetPr(test), data, 9*sizeof(double));
if (!printtestInitialize()){
fprintf(stderr,"Could not initialize the library.\n");
return -2;
}
else
{
int i = 0;
mlfMlprint(1, &out, test);
printf("%s\n",mxArrayToString(out));
mxDestroyArray(out);
out = NULL;
mxDestroyArray(test);
test = NULL;
printtestTerminate();
}
mclTerminateApplication();
return 0;
}
2 Kommentare
Friedrich
am 2 Feb. 2012
Yes and it works fine. This is also documented here:
http://www.mathworks.com/help/toolbox/compiler/f2-998954.html#f2-1008549
E.g. a function like that
function varargout = foo(varargin)
get this C/C++ interface (this is one of multiple interfaces which are generated):
void foo(int nargout, mwArray& varargout,
const mwArray& varargin)
Weitere Antworten (2)
Kaustubha Govind
am 27 Jan. 2012
The easiest way is to just have the MATLAB "disp" function do the job for you:
mexCallMATLAB(0,NULL,1, &in1, "disp");
mexCallMATLAB(0,NULL,1, &in2, "disp");
(The required header is still "mex.h", which all MEX-files need)
However, if you want to do it the old school way in C, you'll need to get the pointer to the built-in representation of the mxArrays using mxGetPr (and mxGetPi if you are dealing with a complex variable).
#include "matrix.h"
...
double *in1pr = mxGetPr(in1);
mexPrintf("in1=%f\n", in1pr[0]);
double *in2pr = mxGetPr(in2);
mexPrintf("in2=%f\n", in2pr[0]);
4 Kommentare
Gauraang Khurana
am 2 Jan. 2016
Thanks Kaustubha, I was struck on this problem for almost 5hours now. I have finally printed the elements. Thanks alot.
Friedrich
am 30 Jan. 2012
Hi,
first of all, mexcallmatlab can be called from a mex file only. Since Mohammed writes an exe, this won't work.
Why are you writing an exe which uses the mxArray API? Do you use the MATLAB Engine? Or do you try to use a MATLAB Compiler generated DLL?
Siehe auch
Kategorien
Mehr zu Deploy to C++ Applications Using mwArray API (C++03) finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!