File Exchange

image thumbnail

Joystick MEX file

version 1.1 (2.28 KB) by

Portable joystick MEX file using SDL



View License

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.

Comments and Ratings (7)

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'

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

It does not compile on 64 architecture, Mac

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).


Peter Corke

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

Bruno Dias

I was unable to compile this file.

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



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.

MATLAB Release
MATLAB 8.0 (R2012b)
Tags Add Tags

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

» Watch video

Win prizes and improve your MATLAB skills

Play today