mex script and how to show output from another function which is using vprintf()
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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
Accepted Answer
OCDER
on 6 Jun 2018
Instead of using vprintf, try using mexPrintf
13 Comments
Walter Roberson
on 6 Jun 2018
Except that the call to vprintf() is inside the other program and the user cannot alter that code.
OCDER
on 6 Jun 2018
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
on 6 Jun 2018
@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
on 6 Jun 2018
Yeah, that's a tough one. Have you asked Mathworks Support directly? They might have a solution.
Walter Roberson
on 6 Jun 2018
Perhaps you can define your own vfprintf() that calls mexprintf. As long as yours is earlier in the link order, yours will be used.
@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
on 6 Jun 2018
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
on 7 Jun 2018
@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
on 7 Jun 2018
can you show me an example with the mex script that I have added above. I do not really get what you are suggesting.
OCDER
on 7 Jun 2018
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
on 7 Jun 2018
@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
on 7 Jun 2018
You're welcome! Glad we can help to get a workaround at least.
More Answers (0)
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)