Interfacing with Matlab

Here is a library of classes for interfacing with mbed from MATLAB. This might be useful for things like:

  • Using MATLAB to aquire and process data from sensors connected to mbed
  • Using MATLAB to control actuators connected to your mbed.
  • Implementing hardware in the loop programs in MATLAB, where the sensors and actuators are connected to mbed but MATLAB carries out the calculations and control.

This page gives examples of two methods of interfacing between MATLAB and mbed.

  • MATLAB RPC This is a set of classes which allows you to control the mbeds inputs and outputs directly from MATLAB. For many applications you may not need to write any mbed code.
  • Serial Comunication These examples show how you can send and receive information over serial in MATLAB and so transfer information between a program running on mbed and MATLAB.



The MATLAB RPC library has been updated to improve the way it works. The classes are now all in a separate namespace. This means that the interface can now be used from within a function, callback or custom class, as well from the command line or within scripts.

Existing scripts will work with the new library but require you to add a line of code to the top of the script

import mbed.*
mymbed= SerialRPC('COM5', 9600);
myled = DigitalOut(mybed, LED1);

Or address classes using the name space

mymbed = mbed.SerialRPC('COM5', 9600);
myled = mbed.DigitalOut(mymbed, mbed.LED1);

To avoid a clash with the namespace name it is recommended that you do not use "mbed" as a variable/object name. A test connection, more robust argument checking and help infomation have also been added. Thanks goto Gautam Vallabha for these improvements.

In this example we control the mbed using the RPC. This makes it possible to remotely create objects and then execute their methods over any transport mechanism by sending the commands as a string. For methods which return a value the mbed returns that value as a string. Here are a set of MATLAB classes which expose a lot of the mbed API.

This has been tested and confimed to work using:

  • R2008a and R2008b (32-bit Windows), with a minor warning
  • R2009a and later (both 32 and 64 bit Windows)

Using the mbed MATLAB classes

To use these classes download the above zip and extract the files to somewhere on your computer. Then add the location of these files to the MATLAB path, the files contain a function "add_mbed_to_path.m" which will do this for you.

The aim in developing these classes has been to mirror the mbed API as closely as possible. This means that they follow the same object oriented approach as when you program on mbed, but with one key difference. In MATLAB you must first create an mbed object and then when you create interface objects pass them that mbed object as this effectively passes information about how the mbed is connected and how to communicate with it. This method will allow you to communicate with multiple mbeds from MATLAB.


To use this library of code for MATLAB your mbed must be running code which can receive and then execute RPC commands. The Interfacing Using RPC page explains this and has some example programs. You can use the RPC_Serial program or the HTTP server with the examples on this page.


The MATLAB classes have been designed to make it feel very similar to programming directly on mbed. Here is the complete MATLAB code required to flash an LED ten times and then close the serial port.


import mbed.*
mymbed = SerialRPC('COM5', 9600)
myled = DigitalOut(mymbed, LED1);
for i = 1:1:10

The RPC library is contained within the "mbed" namespace, this means that you must first either import the mbed package (using "import mbed.*") or prefix all classes with the namespace (eg "mbed.SerialRPC(...)"). More examples of this format can be found in the source files.

The first step when programming for mbed in MATLAB is to create your mbed object. You do this by creating an mbed object from the class for the relevant transport mechanism. This can be Serial or HTTP.

import mbed.*
%Create an mbed Object
mymbed = SerialRPC('COM5', 9600)
mymbed = HTTPRPC('');

You can then create objects from the mbed API, eg AnalogIn, DigitalIn, DigitalOut etc. These must all the passed the mbed object created above and then the arguments for that type of object as defined in the Handbook.

%Creating Interface Objects on mbed from MATLAB
dout1 = DigitalOut(mymbed, p21);
dout2 = DigitalOut(mymbed, LED1);
pwm1 = PwmOut(mymbed, p22);
pwm2 = PwmOut(mymbed, LED2);
ain1 = AnalogIn(mymbed, p20);
device = Serial(mymbed, p13, p14);

If you have objects already created on mbed, say a DigitalOut with the name "myled" then you can tie a MATLAB object to control this object


//Create object in c++ code on mbed
DigitalOut myled(LED1, "myled");


%tie to that object in MATLAB
dout3 = DigitalOut(mymbed,'myled')'

Having created your objects you can now use the methods defined for those objects in the mbed API. However, there isn't the overloading of the operators that you are probably used to from programming the mbed directly. This means that to control a DigitalOut you have to call the "write" method.

%Calling methods from MATLAB
%to set a DigitalOut

%to set PwmOut

%to read an AnalogIn
x =

%to read and write to a serial port
device.puts('data to send out of p13 and p14');
if device.readable == 1
    Data =  device.gets;

Note that the serial class only supports gets, getc, puts, putc, baud and readable/writeable.

As the MATLAB classes actually create objects on the mbed it can become important to reset your mbed before running your MATLAB code as this will prevent there being too many objects or conflicting objects created on the same pin. You can of course do this by pressing the button or alternatively by using the reset function in the mbedSerial class.

%Reset mbed remotely from MATLAB

Finally once you have finished communicating with the mbed then you must delete the mbed object which also closes the serial port so that it will be avaliable to other applications.

%Deleting mbed object and so closing transport mechanism

The mbed MATLAB library includes a test transport mechanism which simulates an mbed connection.

This could be useful for testing the interface code, and potentially to test your own code without needing to connect an mbed, though note for methods which return an argument the test connection will return a random value.

mymbed = mbed.TestRPC();

When you have finished testing your code, all you need to do is change this line of code to the type of transport mechanism you want to use.

Communicating with Custom Code

Most projects require not only simply Digital or Analog I/O but also make use of some of the many libraries for the mbed. These don't support RPC and nor does the MATLAB library provided here include support from them. Instead a mechanism which allows you to directly access variables on mbed or call custom functions is provided by the RPC-Interface-Library. The MATLAB library provided here includes support for these objects.

%Attach to an Existing RPCFunction on mbed
RangeFinder = RPCFunction(mymbed, "RangeFinder");
%Run its methods
range =;

%Read and Write to a variable on mbed
Variable = RPCVariable(mymbed, "f");
f =;


Theres a more complete example of the mbed code and MATLAB script required to gather data from a RangeFinder at the bottom of this page.


If you want to run completly custom RPC reqeusts then you can use the RPC method of the mbed object. The arguments must be passed as an array of strings.

mbed = SerialRPC('COM7', 9600)
mymbed.RPC('myled', 'write', {'1'})

Serial Communication

Using the basic serial over usb communication method, it is possible to interact with Matlab using the Matlab "serial" object. Using this method it is very easy to dump data from mbed into Matlab to be displayed or processed.

The code on the mbed side is exactly the same as you would use to talk to a pc with terraterm or similar. I have been using it to read analogue data from an accellerometer, so basically just dumping data from 3 AnalogIn's over the serial connection, using the code below:


#include "mbed.h"

AnalogIn x(18);
AnalogIn y(19);
AnalogIn z(20);

int main() {
    while(1) {
       printf("%d,%d,%d\n", x.read_mv(), y.read_mv(), z.read_mv());

The Matlab code I have been using basically just reads in the data from the serial port and displays it as a graph. The graph overwrites itself every n samples and the x-labels update to match. The try/catch block is important as if an error occurs while the serial object is instantiated before fclose() is called then the port remains open and cannot be closed. The only solution I have found to this is to restart the PC, so using a try/catch to close the port on an error is important!

function accell()

TIMEOUT = 5;    %time to wait for data before aborting
XPOINTS = 50;   %number of points along x axis

    %create serial object to represent connection to mbed
    mbed = serial('COM4', ...
                  'BaudRate', 9600, ...
                  'Parity', 'none', ...
                  'DataBits', 8, ...
                  'StopBits', 1);       %change depending on mbed configuration
    set(mbed,'Timeout',TIMEOUT);        %adjust timeout to ensure fast response when mbed disconnected
    fopen(mbed);        %open serial connection

    position = 1;       %initialise graph variables
    time = 1;
    x = [(1:XPOINTS)' (1:XPOINTS)' (1:XPOINTS)'];
    xlabels = (1:XPOINTS);
    y = zeros(XPOINTS,3);
    while (1)       %loop forever (or until an error occurs)
        values = fscanf(mbed, '%f,%f,%f');    %get values into vector
                                              %assumes data formatted as
        y(position,:) = values';    %put into y to be displayed
        %update position on x-axis and x-axis labels
        xlabels(position) = time;
        time = time + 1;
        if (position < XPOINTS)
            position = position + 1;
            position = 1;

        set(gca, 'XTick', 1:XPOINTS);
        set(gca, 'XTickLabel', xlabels);

        drawnow;    %this is required to force the display to update before the function terminates

    fclose(mbed);   %close connection (this should never be reached when using while(1), but included for completeness)
    %in case of error or mbed being disconnected
    fclose(mbed);   %close connection to prevent COM port being lokced open

When this is executed (assuming mbed is already churning out data), a plot window appears and is continually updated until mbed is disconnected. At this point, after the timeout period has elapsed, the read fails and the control is safely passed back to the Matlab command line by the catch block. This is certainly not very elegant, but does seem safe and reliable, though I'm sure it must be possible to do this better by sending synchronisation data back and forth across the link.

The code for just reading in the data from mbed into a variable is only a few lines long, the majority of this code is concerned with formatting and displaying the data. Using fscanf the data can be automatically converted from the string receieved back into seperate variables using (I think!) exactly the same syntax as was used in printf on mbed to put it into a string in the first place. The actual important bits of code for doing things are just:

mbed = serial('COM4', ...
                  'BaudRate', 9600, ...
                  'Parity', 'none', ...
                  'DataBits', 8, ...
                  'StopBits', 1);
values = fscanf(mbed, '%f,%f,%f');

There is also an fprintf function which can be used to write data back to mbed over the same serial connection using the same syntax, though I've not played with this yet.

Range Finder Demo

MATLAB can be very useful for processing and manipulating data, particuarly when you have a lot of data points. This example shows how you can use the library above to work with both standard IO and custom code to allow use of many other interfaces such as I2C or SPI sensors This demo uses a SRF08 UltraSonic Ranger mounted on a servo pan tilt mount to record lots of data and then uses MATLAB to process and display this data. This could be mounted on the front of a robot and used by the robot to find its way around its surroundings. Matlab has complete control of the servos and the range finder in real time.

The servos that control the direction of the Range Finder can be controlled directly from MATLAB as they just use PWMOut (though an external power supply needs to be used). The Ranger Finder is read using a RPCFunction object which returns a string consisting of just the measured distance in cm. The actual read is done using the library that has already been written for the SRF08.

The MATLAB script controls the servos, reads from the range finder and plots the points on a graph. The Matlab script ignores points which are out of range. A PTM button is also included on the circuit to demonstrate reading from the a DigitalIn and so that the range finder can be stopped during a scan.


This image shows the results for a singal sweep, there were too boxes infront of the scanner and you can clearly see where the gap is between them. The range finder gives good results but as it has a conical beam shape it tends to see edges as very blured, in this case it extends the edges of the boxes slightly. However the great advatage of having loaded the data from matlab is that it would now be possible to use Matlab to process the data to get a better idea of what objects are where. The points are held in a matirx and so it would be relativly easy to filter or manipulate them in a number ways so that your device could interpret what was in front of it.

As the RangeFinder is mounted on a pan tilt mount the scan above can be repeated at different angles and could then be used to build up a 3D image of the surroundings and the extra data used to refine the accuracy. This is the matlab script used: ScanDemo.m

Coming soon - video of the range finder in action.

Here is a slightly simplified version of the Matlab script which highlights the key parts of the program

import mbed.*
%Set up variables
MaxRange = 80;
MaxServoAngle = 150;
Angles = zeros((READINGS+1), 1);
Range = zeros((READINGS+1), 1);
plotPoints = zeros((READINGS+1), 2);
n = 1;
p = 1;

%Set up communication to mbed
mymbed = SerialRPC('COM7', 9600);
theta_Servo = PwmOut(mymbed, p21);
phi_Servo = PwmOut(mymbed, p22);
StopButton = DigitalIn(mymbed, p5);
srf08 = RPCFunction(mymbed,'RangeFinder');

%Set up the servos

for t = 1:(1/READINGS):2;     
    theta_Servo.pulsewidth((3 - t)/1000);
    %give the servo time to get into position

    %Read the distance into the Range matrix - add 3 cm to account for distance of range finder from pivot.
    Range(n) = str2double(' ')) + 3;
    if Range(n) < 4
       Range(n) = 0;
    if Range(n) > MaxRange
       Range(n) = 0;

    %Calculate the angle    
    Angles(n) = (MaxServoAngle/2) * ((t-1.5) * 2); 
    %Add the points which are valid to the maxtrix
    if Range(n) > 0
       plotPoints(p,1) = pi .* Angles(n) ./ 180 + pi/2;
       plotPoints(p,2) = Range(n);
       p = p + 1;
    n = n + 1; 
    %plot the hits
    polar(plotPoints(:,1), plotPoints(:,2), 'r-');
    if == 0
         display('Stop button pressed');

display('Completed RangeFinder demo');

Here is the code for mbed.

Import program

00001 /**
00002 * Copyright (c)2010 ARM Ltd.
00003 * Released under the MIT License:
00004 */
00005 #include "mbed.h"
00006 #include "SerialRPCInterface.h"
00007 #include "SRF08.h"
00009 using namespace mbed;
00011 //Create the interface on the USB Serial Port
00012 SerialRPCInterface RPC(USBTX, USBRX);
00013 void ReadRange(char * input, char * output);
00014 RPCFunction RangeFinder(&ReadRange, "RangeFinder");
00015 SRF08 srf08(p9, p10, 0xE0);      // Define SDA, SCL pin and I2C address 
00016 DigitalOut myled(LED1);
00018 int main() {
00020     while(1) {
00023         myled = 1;
00024         wait(0.2);
00025         myled = 0;
00026         wait(0.2);
00027     }
00028 }
00030 //As neither I2C nor the SRF08 library is avalible directly over RPC we create a Custom Function which we make RPCable by attaching to an RPCFunction
00031 void ReadRange(char * input, char * output){
00032     //Format the output of the srf08 into the output string
00033     sprintf(output, "%f",;
00034 }


1 related question:


16 Sep 2010

This is some very nice work. I've only tried the serial communications but it looks very promising! One issue I've had though is that eventually, the program will stop due to some error (i.e. the catch block runs). I am sending only a single float value from an accelerometer. And sometimes, when it fails, the port is not released and can't be used anywhere in windows until you restart (as mentioned above) even though the try/catch is in place...

16 Sep 2010

Hi, I'm not sure why the port isn't being released by the try catch block but when the port isn't closed by the program a useful command might be:


This will delete the port if its in use. Alternatively if you have the Instrument Control Toolbox installed then the command instrreset will disconnect and delete all instrument objects

16 Sep 2010

Thanks, I'll give it a try.. Also, is it a long strech to get this to work with simulink. I've used the com port receive block before, but it was very unstable and not as nearly as fast as the matlab rpc here..

18 Oct 2010

Works great. However, I'll having trouble when I paste this into a MATLAB GUI startup function.

mbed connected on Serial port COM3 ??? Undefined function or variable 'LED1'.

Error in ==> mbed_Controller_GUI_test>mbed_Controller_GUI_test_OpeningFcn at 56 myled = PwmOut(mbed, LED1);

Error in ==> gui_mainfcn at 221 feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});

Error in ==> mbed_Controller_GUI_test at 42 gui_mainfcn(gui_State, varargin{:});

I was having some problems earlier on with using this library with the GUI, so I decided to take normally working code and paste it on the opening function. Obviously there's more to it than that. I haven't really used MATLAB GUI all that much so any help is greatly appreciated. (I want to make a MATLAB GUI control panel for my mbed) Thanks.

18 Oct 2010

oh, this is what I pasted in there:

mbed = SerialRPC('COM3', 9600) myled = PwmOut(mbed, LED1); myled.period_ms(20); for k = 1:1:5 for i = 1:1:20 myled.pulsewidth_ms(i); end for i = 20:-1:1 myled.pulsewidth_ms(i); end end mbed.delete; clear;

23 Dec 2010

Hi,I'm sorry but is the first time that I use mbed and in general that I use a microcontroller. I use Matlab for work and so I prefer to write the program in Matlab. I don't understand how to use Matlab, the following step are correct? 1)Download the on computer and add the location of these files to the MATLAB path. 2)Download the rpc_serial_lpc1768.bin on the mbed and hit reset on the microcontroller to start it running. 3)Download the demo leddemo.m, as an example, and save it in the same folder where are the files of 4)Run leddemo.m in Matlab The error is "A class definition must be in an "@" directory". What is the problem? Are the step uncorrect? PS. There is a complete guide to mbed in pdf? Thank you.

23 Dec 2010


The leddemo.m doesn't need to be in the same location as the rest of the files, by adding the location of those files to the path MATLAB will know where they are. So try taking leddemo.m out of and above this folder, also I think you need to add the folder the mbedRPC files are in to the path as opposed to the files themselves.

MATLAB, organising classes in folders

The link above shows how to organise classes into folders, at the moment the mbedRPC library uses path folders as each class is defined within one file. If you can't make this work then you could try separating the classes into @-folders. This may depend on which version of MATLAB you are using. Otherwise it looks like all your steps are correct. Rather than running leddemo.m you could also try just running the commands to connect to mbed etc as above.


23 Dec 2010

Hi, 1)I have created a subfolder D:\toolbox_delia\embed\classes (add to the Matlab path) where there are the files of and I put the file leddemo.m out, in the folder D:\toolbox_delia\embed. 2)I have created, for any file in the subfolder D:\toolbox_delia\embed\classes, a @-folder where I have put inside the file.m that define the class. I don't know if this is necessary, but when I run leddemo.m the error "A class definition must be in an "@" directory" does not appear any more. 3)On the other hand the new error "Undefined function or variable 'private'" appears (File: SerialRPC.m Line: 27 Column: 16).

I think that there are other things wrong in my steps. Moreover I think that to use correct the mbed I have to creat from a file.m a file.bit to load on the mbed but I don't know how to convert a file.m to a file.bin.

Thank you very much.


23 Dec 2010


You do not not need to convert a .m file to a .bin file to use the mbedRPC library above. RPC is explained more in Interfacing Using RPC. On this page the code runs in matlab on the computer and there is small program on mbed which interprets instructions sent to it over the serial port. The program which runs on mbed can be found on the Interfacing Using RPC page. You can use the instructions on that page to check that the mbed is working by using a terminal to send the RPC commands, - note that you will need to have set up the mbed-pc serial port.

I'm wondering what version of matlab you are using. The mbedRPC code was developed and tested in 2010a. The error appears to be with the set access parameter of the class properties. It wouldn't matter too much if you were to remove or change the access specifier other than to be careful that your code doesn't then alter any of the properties of the class.

22 Feb 2011

Mr. Walker,

Thanks for the interfacing! As a student, this is a big discovery for me! I've seen what MATLAB can do as far as Signal Processing and Systems, and it's great stuff.

As a random aside, is it possible to interface MATLAB with an event driven language like ActionScript or VisualBasic? Sorry if I'm sounding ignorant.


23 Apr 2011

Anybody has try it under Unix\Linux

when I try:

>> mbed = serial('/dev/ttyACM0', ...
                 'BaudRate', 9600, ...
                 'Parity', 'none', ...
                 'DataBits', 8, ...
                 'StopBits', 1);
>> fopen( mbed )
??? Error using ==> serial.fopen at 72
Port: /dev/ttyACM0 is not available. No ports are available.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.

Has anyone make it work with linux?

24 Apr 2011

Ernesto 'COM4' is only for windows!!! It's been a few months since I've used Matlab on linux but I think it's 'ttyUSB0' or 'ttyACM0'. Search the matlab help for 'serial' and it will tell you the differences between linux and windows.

24 Apr 2011

lhiggs CSUM wrote:

Ernesto 'COM4' is only for windows!!! It's been a few months since I've used Matlab on linux but I think it's 'ttyUSB0' or 'ttyACM0'. Search the matlab help for 'serial' and it will tell you the differences between linux and windows.

Yes you are right, but the error was on the post, on Matlab I've tried '/dev/ttyACM0' wich works fine with Python, in Matlab it seems to work on '/dev/ttyS0' wich I believe is the equivalent to 'COM0' on windows, but since my compuer doesn't have a built in Serial Port it does nothing, but I CAN fopen('/dev/ttyS0') and it doesn't give me any error.

I also find this message:

From: "Rob Purser"

At this time, MATLAB only supports built-in serial ports and devices that
emulate built in serial ports at the hardware level.
16 May 2011

I am having trouble reading data over the serial line from an I2C temperature sensor connected to pins p13 &14 on the mbed. I am using matlab 2011a on a mac. Here is my code:

import mbed.* sercon=SerialRPC('/dev/tty.usbmodemfa132',9600); temp1=Serial(sercon,p13,p14); a=fopen(temp1); tempdata=fscanf(a,10); fclose(temp1);


Created new Serial on mbed Successfully connected to mbed Objects that can be created or used: obj10000408 obj100003D8 Serial BusInOut BusIn BusOut SPI Timer PwmOut DigitalInOut DigitalOut DigitalIn AnalogOut AnalogIn Base

Created new Serial on mbed ??? Error using ==> fscanf Invalid file identifier. Use fopen to generate a valid file identifier.

Error in ==> tempsensor at 6 tempdata=fscanf(a,10);

14 Jun 2011

hi i have a question about the serial communication, is 'COM4' in the code in this section is used by teraterm for the connection of the mbed or it is a new serial port for displaying the data collected from the mbed to MATLAB ? I will appreciate your response

14 Jun 2011

hi , furthermore when i try to open the port using fopen(mbed) , i got this error (??? Error using ==> serial.fopen at 72 Port: SERIAL-COM3 is not available. Available ports: COM2, COM3. Use INSTRFIND to determine if other instrument objects are connected to the requested device.) or if i use mbed = serial('COM3') fopen(mbed) i got this error (??? Error using ==> serial.fopen at 72 Cannot connect to the COM3 port. Possible reasons are another application is connected to the port or the port does not exist.)

15 Aug 2011


I have a problem with the HTTP Server, it doesn't work. I have taken the program “RPC Over HTTP” on this page but Matlab say:

mymbed = HTTPRPC(''); ??? Undefined function or variable 'address'. Error in ==> HTTPRPC>HTTPRPC.HTTPRPC at 46 if nargin ==0 || ischar(ipAddress) || isempty(regexp(address, '^\d+\.\d+\.\d+\.\d+$', 'once'))

With the HyperTerminal of Windows I can see that the mbed has this IP

For info:

-I use the network of my University and the IP of my computer is

-When I ping the mbed, the answer is good (No “Request timed out” )

-I can turn on (or turn off) the LED with my webbrowser ( 1)

12 Jun 2012

Ernesto Palacios wrote:

Anybody has try it under Unix\Linux

when I try:

>> mbed = serial('/dev/ttyACM0', ...
                 'BaudRate', 9600, ...
                 'Parity', 'none', ...
                 'DataBits', 8, ...
                 'StopBits', 1);
>> fopen( mbed )
??? Error using ==> serial.fopen at 72
Port: /dev/ttyACM0 is not available. No ports are available.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.

Has anyone make it work with linux?

I had the same problem. I followed the steps in the following link (create a java opts file) and now I can communicate. Hope this is helpful

17 Aug 2012

I notice that CAN Objects are not available in this RPC. Any suggestion if i want to carry out the CAN communication in my project which is controlled by Matlab(GUI).

10 Mar 2014


In the serial communication part, i want the x axis to start from 0 and stops at 20 with a step with 0.02 step.

How could i do that ???