MATLAB: Mex usage efficiency question

mexpcl

Hi, I am new to mex. im using mex with PCL (Point Cloud Library). what i want to do is calculate different features of the point cloud. for each feature calculation i have a different mex file.
my question is which of the following two options is more efficient:
  1. Give the mex as input a file name and have it open that file and create the PCL cloud class (the basic structure to do anything) in each of the mexs.
  2. Open the file in MATLAB (or a different mex), store the point data in a MATLAB matrix and pass this matrix to each of the other mex files (and create the cloud class in each mex using this data).
so i guess my question is what would probably be more efficient – reading a file to create a class or manually creating a class every time ?

Best Answer

You don't say definitely whether or not you will need the PCL cloud class data in a MATLAB variable for some reason (maybe some calculations based on m-code). My comments below assume you don't really need the PCL cloud class data on the MATLAB side.
(1) Give the mex as input a file name and have it open that file and create the PCL cloud class (the basic structure to do anything) in each of the mexs.
This approach allows you to build the PCL cloud class in native C++ using presumably already available code. The downside of doing it exactly as you propose, using multiple mex files, is that you need to duplicate the reading and creating of the PCL cloud class for each separate mex routine. I prefer this approach over (2) below, but would change the approach to only one mex routine (see last comment).
(2) Open the file in MATLAB (or a different mex), store the point data in a MATLAB matrix and pass this matrix to each of the other mex files (and create the cloud class in each mex using this data).
The approach requires you to write new code to create a semblance of the PCL cloud class in a MATLAB variable. And each time you want to calculate something with this variable in a mex routine, you will need to copy all of it into its native C++ format first. This is not going to be efficient, and requires new code.
"... for each feature calculation i have a different mex file ..."
If you are looking for efficiency, then I would not do this. Rather, I would have one mex routine with all of the code in it for all of the calculations I wanted to do. When calling the mex routine, pass in an argument that specifies which function you want to do. E.g., this argument could be a string. If you passed in 'create' then it would read a file to create the PCL class variable and store it in a persistent memory block. Other strings would cause calculations to be performed on that PCL class variable. Passing in 'delete' would delete the PCL class variable. Etc. The advantage of this approach is that you only need to read in and create the PCL class variable once, only one copy of it exists in memory, and you can calculate different functions on it without re-reading it into memory. You would need to register a mexAtExit function to automatically do the 'delete' function if the mex file itself was cleared from memory.