Runtime enum-to-type mapping

Both Matlab array mxArray and OpenCV’s Mat can hold data any basic type.  I want to write code which essentially can handle any data type. My current solution is to write template based code for data processing.  I call this code with a switch/case statement. I wanted to avoid rewriting this switch/case for each function that I write.  I was hoping that there was a better way of doing this. My friend posted this for me on stackoverflow.

mxClassID category = mxGetClassID(prhs[0]);
switch (category) {
      case mxINT8_CLASS: computecost<signed char>(T,offT,Offset,CostMatrix); break;
      case mxUINT8_CLASS: computecost<unsigned char>(T,offT,Offset,CostMatrix); break;
      case mxINT16_CLASS: computecost<signed short>(T,offT,Offset,CostMatrix); break;
      case mxUINT16_CLASS: computecost<unsigned short>(T,offT,Offset,CostMatrix); break;
      case mxINT32_CLASS: computecost<signed int>(T,offT,Offset,CostMatrix); break;
      case mxSINGLE_CLASS: computecost<float>(T,offT,Offset,CostMatrix); break;
      case mxDOUBLE_CLASS: computecost<double>(T,offT,Offset,CostMatrix); break;

The details I missed on the stackoverflow question were I plan to call the this function template only at once. However, I have multiple functions which will need this switch case structure. This makes the function table based methods useless and I don’t really want to use macros. So, it seems like there is now way of getting rid of the switch/case.


  1. real problem is, can you hold a large space for some data using spalloc in matlab and using some kind of virtual memory use instead of real memory allocation? matlab always runs in “out of memory” status even for small memory allocation.

Leave a Reply

Your email address will not be published. Required fields are marked *