jump to navigation

Save and run the current m-file at once! December 5, 2009

Posted by viboon in : Matlab , add a comment

Previously I had to switch the windows of Matlab console and text editor back and forth to save and run the code.

Just press F5 on the editor page where a m-code is being edited, the file will be saved and run automatically on the console windows. It is such a convenient and Matlab user should know!.

Tags: ,

Related posts:

A very brief introduction to C implementation in Matlab August 23, 2009

Posted by viboon in : C, Matlab , add a comment

1. Create a file in C format which a heading file of mex.h has to be added in order to interface with Matlab. Instead of staring from main() function, we use mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) to acquire and return the variables between C function and Matlab.

#include "math.h"
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[])
    //Declare the input variable in pointer format
    mxArray *xInput;
    double *xMat, *xAns;
    int row, col;
    //Copy to the pointer
    xInput = prhs[0];
    //Get the matrix
    xMat = mxGetPr(xInput);
    row = mxGetM(xInput);
    col = mxGetN(xInput);
    //Allocate the memory for output value
    plhs[0] = mxCreateDoubleMatrix(row, col, mxREAL);
    //Apply the pointer to the reserved memory
    xAns = mxGetPr(plhs[0]);
    //Start your coding here

2. Compile the C file by using a command below:

mex mycfun.c

3. Call your compiled C function in the M-file.

[xAns] = mycfun(xInput);

4. Enjoy!

Tags: , , , ,

Related posts: