8

I am writing a Matlab mex-file. However, mex-files seem to have a serious limitation: help mexfilename won't cause a help text to appear.

I could circumvent this by writing a m-file, that ultimately calls the mex-file, but includes help, but there has to be a better way.

On the other side, that way I could do all the error-checking in the m-file, where it is far more convenient to do so...

Amro
  • 123,847
  • 25
  • 243
  • 454
bastibe
  • 16,551
  • 28
  • 95
  • 126

2 Answers2

11

I believe PierreBdR is right; you would create an m-file version of your function with just the header call and comment block, but no body.

It might not be a bad idea to put the error checking for the inputs in the m-file, then have the m-file invoke the mex-file (you may have to give them different names, though). It may be more straight-forward to check variables in MATLAB (using, for instance, built-ins like nargchk) and put them into a standard format that you may always want the inputs to the mex function to have. Many of the Image Processing Toolbox functions that I've looked at seem to do this (formatting and checking data in the m-file then doing the expensive computations in a mex-file).

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • 3
    I realise this is old, but having just followed the advice on here - I found that the `m` file had to be in the same folder as the `mex` file, otherwise Matlab gave the empty `m` file priority and did nothing! – n00dle Apr 08 '11 at 14:33
  • 1
    @ianhales: I usually put the M-file (with input arguments parsing and checking) on the path, and the MEX-file (doing the real computation) in an adjacent `private` folder. For example, you'd have `/path/to/myfcn.m` and `/path/to/private/myfcn_mex.m` (assuming `/path/to` is added to the path), with `myfcn` calling the private MEX-function – Amro Aug 21 '12 at 16:38
5

You have to create an m-file (name.m) with the same name as your mex-file (name.c). Then, you put the function declaration and help text, but no function body. Example:

function [o1,o2] = MyFct(i1,i2,i3)
% MyFct takes 3 arguments and returns 2 ...      
Brent
  • 113
  • 6
PierreBdR
  • 42,120
  • 10
  • 46
  • 62