MATLAB Answers

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

6 views (last 30 days)
ThT on 6 Jun 2018
Commented: OCDER on 7 Jun 2018
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);
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


Sign in to comment.

Accepted Answer

OCDER on 6 Jun 2018
Instead of using vprintf, try using mexPrintf


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.

Sign in to comment.

More Answers (0)

Sign in to answer this question.




Translated by