0

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("Give me %i apples.\n", 3);

I am not able to get any output in the matlab's console prompt, like I do when I used printf() or cout instead. I was trying to figure it out myself but no chance so far, thus I would appreciate if someone could explain me what is the issue and how I can bypass it.

Altering the library's files is not possible, thus I am looking for a solution that can be done on mex script from my side.


Update: For example in the following mex script from the printing functions only the PrintInfo() does not show anything:

#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;
  }
ttsesm
  • 917
  • 5
  • 14
  • 28
  • The solution is to not use that function. When you call `printf`, it is automatically converted to a call to `mexPrintf`. That is the right way to print to the MATLAB command window. https://www.mathworks.com/help/matlab/apiref/mexprintf.html – Cris Luengo Jun 07 '18 at 12:04
  • If you use `std::cout` see here: https://stackoverflow.com/a/41276477/7328782 — not sure if this affects `vprintf` too. – Cris Luengo Jun 07 '18 at 12:09
  • I know that `mexPrintf` is the suggested way to use in Matlab, but as I am saying I need to call this `PrintInfo()` function. Thus, I was wandering if possible to make `vprintf()` and its calling function work. I am not sure that I understand the issue with the `cout`, as you can see in my example I am able to use `cout` without problems. – ttsesm Jun 07 '18 at 12:41
  • `cout` works for you? What OS? It doesn’t on Mac. At least not with R2016a. Maybe newer MATLAB’s automatically redirect it? That would make sense. It’s easy enough. `vprintf` writes to file handle 1, which is not attached to anything. Either don’t use that function, or rewrite it. There is no other solution. Sorry. – Cris Luengo Jun 07 '18 at 13:33
  • yes `cout` works here, with Microsoft Visual C++ 2015 compiler and matlab 2017b. So what you are suggesting is to create my own `vprintf.cpp` that calls mexprintf instead and use that? But how this will tell to `PrintInfo()` to use my `vprintf()` instead of the one that is linking where the `PrintInfo()` is defined in the third party library? – ttsesm Jun 07 '18 at 13:41
  • No, I’m telling you that there is no way of doing this, other than changing the 3rd party library or not using that function. – Cris Luengo Jun 07 '18 at 14:05
  • I see :-(. Thanks for the help though. – ttsesm Jun 07 '18 at 16:30

0 Answers0