Search code examples
cmatlabmex

Prompting user to enter number in MEX code


I wonder is there anyway to prompt user to enter integer within MEX code.

Something similar to what input in MATLAB or scanf in C.

I heard about mexCallMATLAB and its use in

 str = mxCreateString("Enter extension:  ");

   mexCallMATLAB(1,&new_number,1,&str,"input");

However I do not really understand what is the point of mxCreateString and what does &str do. I will be really appreciative if anyone can elaborate a little about this or give me another technique to prompt user to enter data.


Solution

  • Let's start from the beginning. mexCallMATLAB calls a MATLAB function, user-defined MATLAB function or MEX file within MEX code. The function declaration is such that:

    int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[], 
                      const char *functionName);
    

    The parameters in detail are:

    1. nlhs: The total number of output parameters that the MATLAB or MEX function is expected to produce.
    2. *plhs[]: An array of pointers where each element is a pointer to an output argument
    3. nrhs: The total number of input parameters that the MATLAB or MEX function is expected to take in.
    4. *prhs[]: An array of pointers where each element is a pointer to an input argument.
    5. functionName: A C string that contains the function name.

    Take note that *plhs[] and *prhs[] must be an array of pointers to MEX-type variables. This is important because this will be used to understand what is going to happen next. Using the above logic, take a look at the call to mexCallMATLAB that you have referenced:

    mexCallMATLAB(1,&new_number,1,&str,"input");
    

    As we can see, the function to call in MATLAB is the input function which is a MATLAB function where the input argument is the string prompt that is used to display in the Command Window before taking in an input from the user and storing this into the output variable. Take note that what is expected is a numerical expression, usually a number or some operation on numbers.

    An example call would look like so:

    out = input('Enter a number: ');
    

    Enter a number: would thus be displayed in the Command Window and whatever number you type in gets stored into the variable out.

    When using mexCallMATLAB, you are doing the equivalent of the above but invoking this in MEX code. There is one input argument into this function and one output argument that is expected. The second parameter is technically a pointer to an output argument where this would be an array of just one element. The output of input will thus be stored in the variable new_number which is going to contain a number. The str variable is a MEX string that is created using mxCreateString. You must create a MEX string because remember that the expected inputs for the input variables for the function to call through mexCallMATLAB must be MEX variables. Therefore, str is a MEX string and &str would be a pointer to a MEX string. This is also technically an array of pointers with one element as well.

    Once this function is called, you put in an input number into the MATLAB Command Window, and thus number is sent back into MEX and stored into new_number in your MEX code.


    This seems to be an elegant way to get a variable from the MATLAB Command Window into MEX. I haven't encountered any other method from what I have seen in my MEX experience, so keep using it!