Documentation Center

  • Trial Software
  • Product Updates

Prompt User for Input

Because MATLAB® does not use stdin and stdout, do not use C/C++ functions like scanf and printf to prompt for user input. The following example shows how to use mexCallMATLAB with the input function to get a number from the user.

#include "mex.h"
#include "string.h"
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  mxArray   *new_number, *str;
  double out;

  str = mxCreateString("Enter extension:  ");
  mexCallMATLAB(1,&new_number,1,&str,"input");
  out = mxGetScalar(new_number);
  mexPrintf("You entered: %.0f ", out);
  mxDestroyArray(new_number);
  mxDestroyArray(str);
  return;
 }

For information about getting user input from a MATLAB function or script, see the input function.

Was this topic helpful?