3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Making a Matlab MEX file

Step 2This is the Code, DO NOT COPY AND PASTE

This is the Code, DO NOT COPY AND PASTE
#######################################################################
........NOTE........
because of some annoying auto-editing that instructables does, the code as seen on this page WILL NOT WORK, you HAVE TO DOWNLOAD THE FILE I attached instead of copying and pasting this.
########################################################################

//This was written in c++ by leevonk
//it is code for a matlab mex file
//the code will take in one number value and output two number values
//the two output numbers are calculated from the input number
//the code will also output a string value
//the code will also print "hello world"

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, mxArray *prhs[])
{

//##########################################
//#######--print "hello world"--############
//##########################################
mexPrintf("Hello world");

//##########################################
//########--Get Stuff From Matlab--#########
//##########################################
/* declare an array variable to hold the incoming vales */
double* InValues;

/* get the values sent from matlab */
InValues = mxGetPr(prhs[0]);

/* to use these values that were sent from matlab, do InValues[0],InValue[1], etc
according to how many values there are. The InValues will be used below */

//##########################################
//########--Return a number array--#########
//##########################################
/* declare the array that'll be sent to matlab (the * makes it an array) */
double* OutValues;

/* Create/allocate return argument, a 1x2 (1 row 2 column) Matrix
for the return array's first slot (plhs[0]) */
plhs[0]=mxCreateDoubleMatrix(1,2,mxREAL);

/* Get pointer to the return argument */
OutValues = mxGetPr(plhs[0]);

/* assign values to OutValues which will reside in the return array's first slot,
here we're using the InValues to computer the OutValues */
OutValues[0] = InValues[0] + 1;
OutValues[1] = InValues[0] + 2;

//###########################################
//###########--Return a String--#############
//###########################################
/* declare the string variable to be sent to matlab */
char* str;

/* assign a value to the string */
str = "byebye";

/* put the string into the return arrays second slot (plhs[1]) */
plhs[1]=mxCreateString(str);

//############################################
//###########--return more stuff--############
//############################################
/* to return more stuff, follow general rules above but put
the stuff into other plhs slots, plhs[somenumber] */

}
« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
81
Followers
40
Author:leevonk
www.leevonk.com