필터 지우기
필터 지우기

print mxArray type.

조회 수: 5 (최근 30일)
Mohammed Samdani
Mohammed Samdani 2012년 1월 27일
댓글: Gauraang Khurana 2016년 1월 2일
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

채택된 답변

Friedrich
Friedrich 2012년 1월 30일
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
Mohammed Samdani
Mohammed Samdani 2012년 2월 2일
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 2012년 2월 2일
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)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Kaustubha Govind
Kaustubha Govind 2012년 1월 27일
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
Mohammed Samdani
Mohammed Samdani 2012년 1월 30일
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 2016년 1월 2일
Thanks Kaustubha, I was struck on this problem for almost 5hours now. I have finally printed the elements. Thanks alot.

댓글을 달려면 로그인하십시오.


Friedrich
Friedrich 2012년 1월 30일
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
Mohammed Samdani
Mohammed Samdani 2012년 1월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Deploy to C++ Applications Using mwArray API (C++03)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by