mex script and how to show output from another function which is using vprintf()

Hi, I am implementing a mex script, where at some point I need to call a function from an external library. This function is as simple as printing the provided input, which is though collected as arguments and passed to vprintf(). See below:
void PrintInfo(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
The issue now is that once I call the PrintInfo() function inside my mex script for example:
PrintInfo("Vector size is: %i \n", myVec.size());
I am not able to get any output in the matlab's console prompt, like I do when I use printf() or cout instead. Does someone has any idea why is this happening.
Since it is not possible to alter the library's code, I would like to find a solution within the mex script.
Update: my working environment is windows if that makes any difference

 採用された回答

OCDER
OCDER 2018 年 6 月 6 日

2 投票

Instead of using vprintf, try using mexPrintf

13 件のコメント

Walter Roberson
Walter Roberson 2018 年 6 月 6 日
Except that the call to vprintf() is inside the other program and the user cannot alter that code.
Oh yes, that does make it tricky - not sure how to get an external print to show up on Matlab's command window display. But it seem the PrintInfo code is being called directly from the MEX function like:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
...
PrintInfo("Vector size is: %i \n", myVec.size());
}
If PrintInfo is a wrapper for vprintf, and that's similar to mexPrintf, then couldn't this work?:
mexPrintf("Vector size is: %i \n", myVec.size());
But if another function is calling PrintInfo like this, not sure what to do...:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
...
OtherFunc("Vector size is: %i \n", myVec.size());
//If this other function calls PrintInfo or vprintf, not sure what to do
}
Is the PrintInfo the same as the example WriteFormatted from here? http://www.cplusplus.com/reference/cstdio/vprintf/
ThT
ThT 2018 年 6 月 6 日
@OCDER as Walter Roberson mentions the call to vprintf() is inside the external library's code which I cannot alter. Thus, I was trying to figure out why this is happening. I am aware of mexPrintf and the other printing functions in mex scripting, but in this case they do not help me since I need to call the PrintInfo() function. The issue is that I created some testing functions where I replaced the vprintf() printing functionality with printf() or cout and this works without issues. Therefore, the question is why vprintf() makes any difference and how I can fix that.
OCDER
OCDER 2018 年 6 月 6 日
Yeah, that's a tough one. Have you asked Mathworks Support directly? They might have a solution.
Walter Roberson
Walter Roberson 2018 年 6 月 6 日
Perhaps you can define your own vfprintf() that calls mexprintf. As long as yours is earlier in the link order, yours will be used.
ThT
ThT 2018 年 6 月 6 日
編集済み: ThT 2018 年 6 月 6 日
@OCDER yes the PrintInfo() code is being called directly from the MEX function the way you wrote it, even if I include it within the same file I cannot get the output. For example in the complete mex script below only the function with the vprintf() does print anything to the console:
#include "mex.h"
// cpp system headers
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdarg>
using namespace std;
void PrintInfo(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
void foo()
{
printf("Vector size is: %i \n", 5);
}
void foo1(const char *format, int sz)
{
printf(format, sz);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
PrintInfo("Vector size is: \n", 10);
foo();
foo1("Vector size is: %i \n", 2);
cout << "Vector size is:" << 4 << endl;
}
I know that I can achieve the same with mexPrintf(), but I need to use the function with the vprintf() from the existing library if possible, thus defining my own vprintf() I do not think that it can help. There should be an explanation and workaround, I cannot believe that it is not possible. There should be something that I am missing.
Walter Roberson
Walter Roberson 2018 年 6 月 6 日
Unless the existing library defines its own vprintf(), then it will be pulling vprintf from a library such as libc, and that libc will be one of the last things linked on the line. If you link your own vprintf definition after the third party library but before libc then the reference to vprintf in the third party library will be satisfied by the vprintf that you provide, and your code can call mexprintf .
ThT
ThT 2018 年 6 月 7 日
@Walter Roberson, I am not sure I understand what you are saying. The PrintInfo() function is defined inside the external library let's say in Core/Core.h, the library is already compiled and then I am including it in my mex script as follows:
mex myMexFile.cpp -L"<path_to_lib>" -lCore -I<path_to_headers_folder>
In my mex file of course I am including Core.h and thus I can call the PrintInfo() function. How defining my own vprintf() with mexPrintf() will make PrintInfo() to call my own vprintf() instead of how it is defined in the third party lib. In the simple example that I have shown above, this is possible I agree. But in the current case where PrintInfo() is already defined in the third party library I do not think that this is possible, correct if I am wrong.
Create your own vprintf.c that calls mexprintf, and then
mex myMexFile.cpp -L"<path_to_lib>" -lCore -I<path_to_headers_folder> vprintf.c
ThT
ThT 2018 年 6 月 7 日
can you show me an example with the mex script that I have added above. I do not really get what you are suggesting.
Would something like this work?
#include "mex.h"
// cpp system headers
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdarg>
using namespace std;
template<typename... Args>
void vprintf(const char *format, Args... args) {
mexPrintf(format, args...);
}
template<typename... Args>
void PrintInfo(const char *format, Args... args) {
vprintf(format, args...);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
mexPrintf("mexPrintf: Vector size is: %i, %i, %i\n", 1, 2, 3);
vprintf("vprintf: Vector size is: %i, %i, %i\n", 4, 5, 6);
PrintInfo("PrintInfo: Vector size is: %i, %i, %i\n", 7, 8, 9);
}
ThT
ThT 2018 年 6 月 7 日
@OCDER thanks for the example, it's not exactly for what I was hopping for but it seems to do the job. In any case it a workaround for sure. Thank you for your time.
OCDER
OCDER 2018 年 6 月 7 日
You're welcome! Glad we can help to get a workaround at least.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

製品

リリース

R2017b

質問済み:

ThT
2018 年 6 月 6 日

コメント済み:

2018 年 6 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by