Most basic problem of all, Hello world


William Handler
07-24-2008, 02:25 PM
Hi all

I have written a few mex functions to implement some fast c code ion matlab for various electromagnetic calculations we do. No problem. I got a recent copy of NR3 with the source code so I could start to include some NR3 code in out matlab code set and I have run int the most basic problem of all. I cannot get the hello world routine to do its magic.

INFORMATION:

matlab 2007b, mac os x leopard 10.5.4

The code looks like this...

/* helloworld2.cpp */
#include "nr3matlab.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
MatDoub indata(prhs[0]);
Int i, j, k=0, m=indata.nrows(), n=indata.ncols();
VecDoub outdata(m*n,plhs[0]);
for (i=0;i<m;i++) for (j=0;j<n;j++) outdata[k++] = SQR(indata[i][j]);
printf("Hello, world!\n");
}


The math part works whenI run it from matlab, but no hello world. Also, I did have to change the nr3matlab.h file to get it to compile as it complained about the typeinfo header file. I changed that line from

#include <typeinfo.h>

to

#include <typeinfo>

BY the way, I think this is a good forum concept and I will post my wrappers and makefile for the mex functions as I get them done. That is if I ever get helloworld to work.........

William Handler
07-24-2008, 02:41 PM
It seems to be just the printf that does not work, cout or mexPrintf both work so I am just going to go ahead and use c++ standards for IO even though I have always previously preffered the old c way. C'est la vie. I am still interested if anyone fixes this.

Bill Press
07-28-2008, 06:52 PM
That's odd, because your code looks just like the code shown at http://nr.com/nr3_matlab.html#hwn, and it did work for me.

Some C++ compilers (but not others) are choosy about mixing old-style printf with new-style cout. Is there any way you have an option set that is automatically including the new-style I/O header files?

Another idea would be to macro printf to mexPrintf in the nr3matlab.h file (I haven't tried this.)

Cheers,
Bill P.

William Handler
07-30-2008, 08:59 AM
Thanks Bill

Good to know it worked for you. But did you do this on Leopard OSX?

I assumed it was something to do with the nr3matlab.h header file that screwed things up. It is important to note that the compiler does not complain, it does not crash, it just does not produce the hello world output.
I use printf with my compiler pretty exclusively elsewhere with no difficulty so I am not sure about it being related to the compiler as such, it is much more likely to do with the way the stdout is linked to matlab on the mac os x operating system..

I would rather not use the printf statement if I am really using mexPrintf, as I think that could lead to some confusion further down the line. The easiest answer for me was to use c++ IO and not worry about it.

Thanks for the feedback

Will

jhearn
01-07-2011, 10:33 AM
I had the same problem. Changing <typeinfo.h> to <typeinfo> fixed it, but I'd be happier if I knew why that was necessary. My C++ is pretty rusty.

Is there a "better" fix? Any updates on this issue?

Mac OS X 10.6.6
GCC v 4.2.1
MatLab R2010b