---
title: "Get started with Matlab"
description: "Load UnderAutomation.Robotics in Matlab with NET.addAssembly: Matlab versions and operating systems, a first script on a Fanuc then a Yaskawa controller, the license key of each brand, from .NET to Matlab, known limits."
---

# Get started with Matlab

This page shows how to use UnderAutomation.Robotics from Matlab, to read and control a Fanuc, Yaskawa, Universal Robots, ABB or Stäubli controller from a Matlab script with one API. Matlab loads the .NET DLL of the library with its .NET interface: nothing else is installed on the PC, no software of the robot maker and no driver.

## Requirements

| Item            | Supported                                                                                                                                     |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| Windows         | Matlab with the .NET Framework 4.6.2 or later (the default on Windows 10 and 11). Load the `net48` DLL. Not tested by UnderAutomation with this library yet |
| Linux and macOS | Matlab R2024b or later with the .NET runtime, selected by `dotnetenv("core")`, and the `netstandard2.1` DLL. Not tested by UnderAutomation with this library yet |
| Controllers     | Fanuc, Yaskawa, Universal Robots, ABB and Stäubli: see [Connect to your robot](/robotics/documentation/connect)                                |

Matlab documents its .NET interface in [Call .NET from MATLAB](https://www.mathworks.com/help/matlab/using-net-libraries-in-matlab.html).

## Load the library

### On Windows

1. Download [UnderAutomation.Robotics.zip](https://github.com/underautomation/Robotics.NET/releases/latest/download/UnderAutomation.Robotics.zip) from the latest release of [Robotics.NET](https://github.com/underautomation/Robotics.NET/releases/latest).
2. Unblock the zip before you extract it: right click, `Properties`, check `Unblock`.
3. Copy its `net48` folder next to your script.
4. Load the DLL with `NET.addAssembly`, then import the namespaces you use.

**Matlab : MatlabLoadAssembly**
```matlab
%%
% The net48 folder of UnderAutomation.Robotics.zip, copied next to this script
dll = fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll');

if ~NET.isNETSupported
    error('No supported .NET Framework found on this computer');
end

% Load the library once per Matlab session
NET.addAssembly(dll);

% The common API, then the namespace of each brand you connect
import UnderAutomation.Robotics.Controllers.*
import UnderAutomation.Fanuc.*
%%
```

`NET.addAssembly` needs an absolute path: `fullfile(pwd, ...)` builds one that works on every operating system. Matlab cannot unload a .NET assembly: restart Matlab to load a new version of the DLL.

Do not load a brand DLL (`UnderAutomation.Fanuc.dll`...) in the same Matlab session: Robotics contains the same types. See [Migrate from a brand SDK](/robotics/documentation/migrate-from-brand-sdk).

### On Linux and macOS

Matlab R2024b and later can use the .NET runtime instead of the .NET Framework. Set the environment variable `DOTNET_ROOT` to the folder of the .NET runtime, call `dotnetenv("core")` before `NET.addAssembly`, and load the DLL of the `netstandard2.1` folder. See [dotnetenv](https://www.mathworks.com/help/matlab/ref/dotnetenv.html) in the Matlab documentation.

## First program

### Connect and read the identity of the controller

Create a `RobotController`, connect it with the connection parameters of the brand and read a value.

**C# : GetStartedFirstProgram**
```csharp
using UnderAutomation.Fanuc;
using UnderAutomation.Robotics.Controllers;

public class GetStartedFirstProgram
{
    static void Main()
    {
        /**/
        // No key is needed during the 30 day trial.
        // After the trial, register the license key of each brand once, before the first connection.
        // FanucRobot.RegisterLicense("YourCompanyName", "YOUR_FANUC_LICENSE_KEY");

        // The brand comes from the type of the parameters: here a Fanuc controller
        RobotController robot = new RobotController();
        robot.Connect(new ConnectionParameters("192.168.0.1"));

        // Identity of the controller
        RobotControllerInfo info = robot.Info.Read();
        Console.WriteLine($"{info.BrandName} {info.Model}, software {info.SoftwareVersion}");

        robot.Disconnect();
        /**/
    }
}
```

**Python : GetStartedFirstProgram**
```python
from underautomation.fanuc.connection_parameters import ConnectionParameters
from underautomation.robotics.controllers.robot_controller import RobotController

##
# No key is needed during the 30 day trial.
# After the trial, register the license key of each brand once, before the first connection.
# from underautomation.fanuc.fanuc_robot import FanucRobot
# FanucRobot.register_license("YourCompanyName", "YOUR_FANUC_LICENSE_KEY")

# The brand comes from the type of the parameters: here a Fanuc controller
robot = RobotController()
robot.connect(ConnectionParameters("192.168.0.1"))

# Identity of the controller
info = robot.info.read()
print(f"{info.brand_name} {info.model}, software {info.software_version}")

robot.disconnect()
##
```

**Matlab : GetStartedFirstProgram**
```matlab
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll'));
import UnderAutomation.Robotics.Controllers.*

%%
% No key is needed during the 30 day trial.
% After the trial, register the license key of each brand once, before the first connection.
% UnderAutomation.Fanuc.FanucRobot.RegisterLicense('YourCompanyName', 'YOUR_FANUC_LICENSE_KEY');

% The brand comes from the type of the parameters: here a Fanuc controller
robot = RobotController();
robot.Connect(UnderAutomation.Fanuc.ConnectionParameters('192.168.0.1'));

% Identity of the controller
info = robot.Info.Read();
fprintf('%s %s, software %s\n', char(info.BrandName), char(info.Model), char(info.SoftwareVersion));

robot.Disconnect();
%%
```

The brand comes from the class of the connection parameters. Here, `UnderAutomation.Fanuc.ConnectionParameters` connects to a Fanuc controller. The other classes are in the table of [Get started with .NET](/robotics/documentation/get-started-net).

### The same code for another brand

**C# : GetStartedTwoBrands**
```csharp
using UnderAutomation.Robotics.Controllers;

public class GetStartedTwoBrands
{
    static void Main()
    {
        /**/
        // A cell with a Fanuc and a Yaskawa controller: only the connection parameters change
        RobotConnectionParameters[] cell =
        {
            new UnderAutomation.Fanuc.ConnectionParameters("192.168.0.1"),
            new UnderAutomation.Yaskawa.ConnectParameters("192.168.0.2"),
        };

        foreach (RobotConnectionParameters parameters in cell)
        {
            // The same code for both brands
            RobotController robot = new RobotController();
            robot.Connect(parameters);

            RobotControllerInfo info = robot.Info.Read();
            Console.WriteLine($"{info.BrandName} {info.Model}, software {info.SoftwareVersion}");

            robot.Disconnect();
        }
        /**/
    }
}
```

**Python : GetStartedTwoBrands**
```python
from underautomation.fanuc.connection_parameters import ConnectionParameters
from underautomation.yaskawa.connect_parameters import ConnectParameters
from underautomation.robotics.controllers.robot_controller import RobotController

##
# A cell with a Fanuc and a Yaskawa controller: only the connection parameters change
cell = [
    ConnectionParameters("192.168.0.1"),
    ConnectParameters("192.168.0.2"),
]

for parameters in cell:
    # The same code for both brands
    robot = RobotController()
    robot.connect(parameters)

    info = robot.info.read()
    print(f"{info.brand_name} {info.model}, software {info.software_version}")

    robot.disconnect()
##
```

**Matlab : GetStartedTwoBrands**
```matlab
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll'));
import UnderAutomation.Robotics.Controllers.*

%%
% A cell with a Fanuc and a Yaskawa controller: only the connection parameters change
cellParameters = { ...
    UnderAutomation.Fanuc.ConnectionParameters('192.168.0.1'), ...
    UnderAutomation.Yaskawa.ConnectParameters('192.168.0.2')};

for i = 1:numel(cellParameters)
    % The same code for both brands
    robot = RobotController();
    robot.Connect(cellParameters{i});

    info = robot.Info.Read();
    fprintf('%s %s, software %s\n', char(info.BrandName), char(info.Model), char(info.SoftwareVersion));

    robot.Disconnect();
end
%%
```

### Brand and address from a configuration file

`RobotConnectionParameters.Create` creates the connection parameters from the name of the brand and the address. Its third argument is the routing text, optional in C#: Matlab passes it.

**C# : ConnectFromName**
```csharp
using UnderAutomation.Robotics.Controllers;

public class ConnectFromName
{
    static void Main()
    {
        /**/
        // Brand and address from a configuration file: fanuc, yaskawa, universalrobots, abb or staubli
        string brand = "yaskawa";
        string address = "192.168.0.2";

        RobotController robot = new RobotController();
        robot.Connect(RobotConnectionParameters.Create(brand, address));

        // The same code works for every brand
        Console.WriteLine(robot.Info.Read().Model);

        robot.Disconnect();
        /**/
    }
}
```

**Python : ConnectFromName**
```python
from underautomation.robotics.controllers.robot_controller import RobotController
from underautomation.robotics.controllers.robot_connection_parameters import RobotConnectionParameters

##
# Brand and address from a configuration file: fanuc, yaskawa, universalrobots, abb or staubli
brand = "yaskawa"
address = "192.168.0.2"

robot = RobotController()
# The last argument is the routing settings as text: empty for no setting
robot.connect(RobotConnectionParameters.create(brand, address, ""))

# The same code works for every brand
print(robot.info.read().model)

robot.disconnect()
##
```

**Matlab : ConnectFromName**
```matlab
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll'));
import UnderAutomation.Robotics.Controllers.*

%%
% Brand and address from a configuration file: fanuc, yaskawa, universalrobots, abb or staubli
brand = 'yaskawa';
address = '192.168.0.2';

% The third argument is the routing text: '' for no setting
robot = RobotController();
robot.Connect(RobotConnectionParameters.Create(brand, address, ''));

% The same code for each brand: Info.Read has a route on the five brands
info = robot.Info.Read();
disp(char(info.Model));

robot.Disconnect();
%%
```

### License

Each brand runs for 30 days without a key, from the first use of the library. After the trial, register the license key of each brand you connect, once per Matlab session, before the first connection. Robotics has no license of its own: see [Licensing](/robotics/documentation/license).

**C# : License**
```csharp
using UnderAutomation.Fanuc;
using UnderAutomation.Yaskawa;

public class License
{
    static void Main()
    {
        /**/
        // One key per brand, registered once at startup, before the first connection
        FanucRobot.RegisterLicense("YourCompanyName", "YOUR_FANUC_LICENSE_KEY");
        YaskawaRobot.RegisterLicense("YourCompanyName", "YOUR_YASKAWA_LICENSE_KEY");

        // Each brand has its own license state
        Console.WriteLine($"Fanuc: {FanucRobot.LicenseInfo.State}");
        Console.WriteLine($"Yaskawa: {YaskawaRobot.LicenseInfo.State}");

        // Number of trial days left for a brand without a key
        int? evaluationDaysLeft = YaskawaRobot.LicenseInfo.EvaluationDaysLeft;
        /**/

        /**/
        // Check the brands of your cell once at startup,
        // rather than catching the exception on every connection
        if (!FanucRobot.LicenseInfo.IsLicensed)
        {
            // A readable description of the state
            Console.WriteLine(FanucRobot.LicenseInfo);
            return;
        }
        /**/
    }
}
```

**Python : License**
```python
from underautomation.fanuc.fanuc_robot import FanucRobot
from underautomation.yaskawa.yaskawa_robot import YaskawaRobot

##
# One key per brand, registered once at startup, before the first connection
FanucRobot.register_license("YourCompanyName", "YOUR_FANUC_LICENSE_KEY")
YaskawaRobot.register_license("YourCompanyName", "YOUR_YASKAWA_LICENSE_KEY")

# Each brand has its own license state
print(f"Fanuc: {FanucRobot.license_info.state}")
print(f"Yaskawa: {YaskawaRobot.license_info.state}")

# Number of trial days left for a brand without a key
evaluation_days_left = YaskawaRobot.license_info.evaluation_days_left
##

##
# Check the brands of your cell once at startup,
# rather than catching the exception on every connection
if not FanucRobot.license_info.is_licensed:
    # A readable description of the state
    print(FanucRobot.license_info)
    raise SystemExit(0)
##
```

**Matlab : License**
```matlab
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll'));
import UnderAutomation.Fanuc.*
import UnderAutomation.Yaskawa.*

%%
% One key per brand, registered once per Matlab session, before the first connection
FanucRobot.RegisterLicense('YourCompanyName', 'YOUR_FANUC_LICENSE_KEY');
YaskawaRobot.RegisterLicense('YourCompanyName', 'YOUR_YASKAWA_LICENSE_KEY');

% Each brand has its own license state
fanucInfo = FanucRobot.LicenseInfo;
yaskawaInfo = YaskawaRobot.LicenseInfo;
disp(char(fanucInfo.ToString()));
disp(char(yaskawaInfo.ToString()));

% Check the brands of your cell once at startup
if ~fanucInfo.IsLicensed
    error(char(fanucInfo.ToString()));
end
%%
```

## From .NET to Matlab

The names of the classes, methods and properties are the .NET names of the other pages. The C# samples of the documentation translate line by line, with these rules:

| .NET                                           | Matlab                                                                          |
| ---------------------------------------------- | ------------------------------------------------------------------------------- |
| `new RobotController()`                        | `RobotController()`, after `import UnderAutomation.Robotics.Controllers.*`      |
| `new ConnectionParameters("192.168.0.1")`      | `UnderAutomation.Fanuc.ConnectionParameters('192.168.0.1')`, with the full name |
| `"192.168.0.1"`                                | `'192.168.0.1'`, a char vector                                                  |
| An optional argument, like `group`             | Pass every argument: `robot.Motion.ReadPose(int32(1))`                          |
| An `int` argument                              | `int32(1)`                                                                      |
| `values[0]`                                    | `values(1)`: Matlab indexes .NET arrays from 1                                  |
| A `double[]` result, like `JointValues.Values` | `double(values)` converts it to a Matlab vector                                 |
| A `string` result                              | `char(result)` gives a Matlab char vector                                       |
| An enumeration value, like `StateFeature.Read` | `StateFeature.Read`, after `import UnderAutomation.Robotics.Controllers.*`      |
| `catch (FeatureNotSupportedException ex)`      | `catch e`, then `e.ExceptionObject` gives the .NET exception                    |
| `try { } finally { }`                          | `try ... catch`, then the cleanup in both branches                              |

## Examples

### Read the position

**C# : MotionReadPose**
```csharp
using UnderAutomation.Robotics.Controllers;
using UnderAutomation.Robotics.Geometry;

public class MotionReadPose
{
    static void Main()
    {
        RobotController robot = new RobotController();
        robot.Connect(RobotConnectionParameters.Create("fanuc", "192.168.0.1"));

        /**/
        // Group 1 by default
        RobotPosition pose = robot.Motion.ReadPose();
        Console.WriteLine($"X {pose.Pose.X}, Y {pose.Pose.Y}, Z {pose.Pose.Z}");

        JointValues joints = robot.Motion.ReadJoints();
        Console.WriteLine(string.Join(", ", joints.Values));
        /**/

        robot.Disconnect();
    }
}
```

**Python : MotionReadPose**
```python
from underautomation.robotics.controllers.robot_controller import RobotController
from underautomation.robotics.controllers.robot_connection_parameters import RobotConnectionParameters

robot = RobotController()
robot.connect(RobotConnectionParameters.create("fanuc", "192.168.0.1", ""))

##
# Group 1 by default
pose = robot.motion.read_pose()
print(f"X {pose.pose.x}, Y {pose.pose.y}, Z {pose.pose.z}")

joints = robot.motion.read_joints()
print(", ".join(str(value) for value in joints.values))
##

robot.disconnect()
```

**Matlab : MotionReadPose**
```matlab
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll'));
import UnderAutomation.Robotics.Controllers.*

robot = RobotController();
robot.Connect(RobotConnectionParameters.Create('fanuc', '192.168.0.1', ''));

%%
% Motion group 1: Matlab passes every argument, also the optional ones
position = robot.Motion.ReadPose(int32(1));
pose = position.Pose;
fprintf('X %.3f, Y %.3f, Z %.3f\n', pose.X, pose.Y, pose.Z);

% Values is a .NET array: double() converts it to a Matlab vector
joints = robot.Motion.ReadJoints(int32(1));
disp(double(joints.Values));
%%

robot.Disconnect();
```

`ReadJoints` is not available on every brand: see [Motion](/robotics/documentation/motion).

### Check a method before you call it

A method that the brand does not have throws `FeatureNotSupportedException`. `IsAvailable` returns false before the call. See [Errors](/robotics/documentation/errors).

**C# : RoutingAvailability**
```csharp
using UnderAutomation.Robotics.Controllers;

public class RoutingAvailability
{
    static void Main()
    {
        RobotController robot = new RobotController();
        robot.Connect(RobotConnectionParameters.Create("staubli", "192.168.0.254"));

        /**/
        // False when the brand has no route for the method, or no protocol of the route answered
        if (robot.State.IsAvailable(StateFeature.Read))
        {
            Console.WriteLine(robot.State.Read().Mode);
        }

        try
        {
            robot.State.Read();
        }
        catch (FeatureNotSupportedException ex)
        {
            Console.WriteLine($"{ex.Feature} is not supported on {ex.BrandName}");
        }
        catch (RouteUnavailableException ex)
        {
            Console.WriteLine($"{ex.Feature}: {ex.Message}");
        }
        /**/

        robot.Disconnect();
    }
}
```

**Python : RoutingAvailability**
```python
from underautomation.robotics.controllers.robot_controller import RobotController
from underautomation.robotics.controllers.robot_connection_parameters import RobotConnectionParameters
from underautomation.robotics.controllers.state_feature import StateFeature
from UnderAutomation.Robotics.Controllers import FeatureNotSupportedException, RouteUnavailableException

robot = RobotController()
robot.connect(RobotConnectionParameters.create("staubli", "192.168.0.254", ""))

##
# False when the brand has no route for the method, or no protocol of the route answered
if robot.state.is_available(StateFeature.Read):
    print(robot.state.read().mode.name)

try:
    robot.state.read()
except FeatureNotSupportedException as ex:
    # The exceptions come from the .NET runtime, so their members keep their original names
    print(f"{ex.Feature} is not supported on {ex.BrandName}")
except RouteUnavailableException as ex:
    print(f"{ex.Feature}: {ex.Message}")
##

robot.disconnect()
```

**Matlab : RoutingAvailability**
```matlab
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll'));
import UnderAutomation.Robotics.Controllers.*

robot = RobotController();
robot.Connect(RobotConnectionParameters.Create('staubli', '192.168.0.254', ''));

%%
% False when the brand has no route for the method, or no protocol of the route answered
if robot.State.IsAvailable(StateFeature.Read)
    state = robot.State.Read();
    disp(char(state.Mode.ToString()));
end

try
    robot.State.Read();
catch e
    % ExceptionObject is the .NET exception, for example a FeatureNotSupportedException
    if isa(e, 'NET.NetException')
        disp(char(e.ExceptionObject.Message));
    else
        rethrow(e);
    end
end
%%

robot.Disconnect();
```

## Known limits

- Matlab has no named arguments and no optional arguments: pass all the arguments of a .NET method, in order.
- An exception of the library is a `NET.NetException` in Matlab. Its `ExceptionObject` property holds the .NET exception (`RobotControllerException`, `FeatureNotSupportedException`...), with its `Message` and its properties.
- The methods with a progress callback, like `ReadBytesWithProgress` of `robot.File`, need a .NET delegate. Prefer the methods without a callback, like `ReadBytes`.
- `RobotController` has no event. The objects of the brand SDKs, given by `robot.Native`, have some: a .NET event needs `addlistener`, and its handler runs on a thread of the library. Prefer to read the values in a loop.
- Matlab keeps the DLL loaded until it closes.

See also the Matlab pages on the limits of [.NET arrays](https://www.mathworks.com/help/matlab/matlab_external/limitations-to-support-of-net-arrays.html), [.NET methods](https://www.mathworks.com/help/matlab/matlab_external/limitations-to-support-of-net-methods.html) and [.NET enumerations](https://www.mathworks.com/help/matlab/matlab_external/limitations-to-net-enumerations.html).

## What to read next

- [Connect to your robot](/robotics/documentation/connect): the connection parameters of each brand and the settings of the controllers.
- [Work without a robot](/robotics/documentation/simulator): the simulators of the robot makers.
- [Common API](/robotics/documentation/common-api): the services of `RobotController`.
- [Routing](/robotics/documentation/routing): which protocol each method uses on each brand.
- [Licensing](/robotics/documentation/license): the trial and the license key of each brand.