View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Be the first to rate this file! 6 Downloads (last 30 days) File Size: 2.28 KB File ID: #41041 Version: 1.1

Joystick MEX file



01 Apr 2013 (Updated )

Portable joystick MEX file using SDL

| Watch this File

File Information

A MEX function that returns joystick axes and buttons. It should be reasonably portable since it's based on SDL which runs on Mac, Linux, Win.

>> [j,b] = joystick

j =

         0 0 -0.0039 0.0039

b =

     0 0 0 0 0 0 0 0 0 0 0 0

Could be wrapped into an S-function.

Simulink comes with similar functionality if you have the 3D Animation Toolbox.

Tested on R2012b MacOS 10.8.2 (ML) with SDL 1.2.15.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Other requirements Needs SDL (Simple DirectMedia Layer) library 1.2.15
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
02 Mar 2016 Ahmad Seyfi

I was trying to compile this on OSX El Capitan, Matlab R2015b, Xcode 7.2.1 and SDL2, but I had some errors. Matlab was using "Xcode with Clang" for compilation.

I fixed the issue as follows:
1) I changed the following lines
#include "SDL.h"
#include "SDL_joystick.h"
#include "SDL_video.h"

to these:
#include "SDL2/SDL.h"
#include "SDL2/SDL_joystick.h"
#include "SDL2/SDL_video.h"

2) I changed the compile command to
mex -v joystick/joystick.c LINKLIBS='\$LINKLIBS -framework SDL2'

Comment only
27 Jun 2014 Mohamed Abdelkader Zahana

How do you use it with the 'mex' function in MATLAB command line?

Comment only
21 May 2014 Mohamed Abdelkader Zahana

It does not compile on 64 architecture, Mac

Comment only
31 Mar 2014 Bruno Dias

Thank you for your promptness of your response.

I'm using Matlab 7.6 - R2008a and Windows 32-bit. I did download the SDL libraries (
The error messages are:
>> mex joystick.c
Error C:\Users\myPC\Desktop\Joystick\joystick.c: C:\Users\myPC\Desktop\Joystick\SDL_endian.h: 171 compiler error in _kids--Bad rule number 0

C:\PROGRA~1\MATLAB\R2008A\BIN\MEX.PL: Error: Compile of 'joystick.c' failed.

??? Error using ==> mex at 207
Unable to complete successfully.

I think my problem is the MEX compiler. I tried to use Lcc-win32 compiler (C:\PROGRA~1\MATLAB\R2008a\sys\lcc\bin).

Anyway, I already have got access to the joystick inputs. I'll use the vrjoystick functions (Virtual Reality Toolbox).


Comment only
31 Mar 2014 Peter Corke

Bruno, could you share some details of the error messages.

Comment only
30 Mar 2014 Bruno Dias

I was unable to compile this file.

Comment only
01 Apr 2013 christie harper

Tried on Linux
1) need to replace // with /*
2) variable event not defined

Comment only
03 Apr 2013 1.1

Fixed bug described by Christie, also includes release of dynamically allocated memory.

The // comment problem can be fixed by specifying the C99 compiler, but is probably platform/compiler specific.

Contact us