print mxArray type.

9 views (last 30 days)
Mohammed Samdani
Mohammed Samdani on 27 Jan 2012
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

Accepted Answer

Friedrich
Friedrich on 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 Comments
Mohammed Samdani
Mohammed Samdani on 2 Feb 2012
Wanted to know if any body has tried below functions to pass and collect data from dll using varargin & varargout.
// Outputs
mxArray** plhs[] = { ..., };
//Inputs
mxArray* prhs[] = { };
Friedrich
Friedrich on 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)

Sign in to comment.

More Answers (2)

Kaustubha Govind
Kaustubha Govind on 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 Comments
Mohammed Samdani
Mohammed Samdani on 30 Jan 2012
Yes I did, have also included #include "matrix.h"
mbuild Operations_C.c lib_Operations_C.lib I had also tried
mbuild -g -v Operations_C.c lib_Operations_C.lib. same error message.
Gauraang  Khurana
Gauraang Khurana on 2 Jan 2016
Thanks Kaustubha, I was struck on this problem for almost 5hours now. I have finally printed the elements. Thanks alot.

Sign in to comment.


Friedrich
Friedrich on 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?
  1 Comment
Mohammed Samdani
Mohammed Samdani on 30 Jan 2012
Hi,
Yes, I am using MATLAB comiler generated DLL.
I create a DLL of m file using
mcc -B csharedlib:lib_Operations_C Operations_C.m
Then I do an
mbuild Operations_C.c lib_Operations_C.lib
Operations_C 5 2.
But cannot print the value of mxArray.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!