Get started with Matlab
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.
- Requirements
- Load the library
- On Windows
- On Linux and macOS
- First program
- Connect and read the identity of the controller
- The same code for another brand
- Brand and address from a configuration file
- License
- From .NET to Matlab
- Examples
- Read the position
- Check a method before you call it
- Known limits
- What to read next
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 |
Matlab documents its .NET interface in Call .NET from MATLAB.
Load the library
On Windows
- Download UnderAutomation.Robotics.zip from the latest release of Robotics.NET.
- Unblock the zip before you extract it: right click,
Properties, checkUnblock. - Copy its
net48folder next to your script. - Load the DLL with
NET.addAssembly, then import the namespaces you use.
% The net48 folder of UnderAutomation.Robotics.zip, copied next to this scriptdll = fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll');if ~NET.isNETSupportederror('No supported .NET Framework found on this computer');end% Load the library once per Matlab sessionNET.addAssembly(dll);% The common API, then the namespace of each brand you connectimport 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.
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 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.
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 controllerrobot = RobotController();robot.Connect(UnderAutomation.Fanuc.ConnectionParameters('192.168.0.1'));% Identity of the controllerinfo = 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.
The same code for another brand
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 changecellParameters = { ...UnderAutomation.Fanuc.ConnectionParameters('192.168.0.1'), ...UnderAutomation.Yaskawa.ConnectParameters('192.168.0.2')};for i = 1:numel(cellParameters)% The same code for both brandsrobot = 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.
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Robotics.dll'));import UnderAutomation.Robotics.Controllers.*% Brand and address from a configuration file: fanuc, yaskawa, universalrobots, abb or staublibrand = 'yaskawa';address = '192.168.0.2';% The third argument is the routing text: '' for no settingrobot = RobotController();robot.Connect(RobotConnectionParameters.Create(brand, address, ''));% The same code for each brand: Info.Read has a route on the five brandsinfo = 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.
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 connectionFanucRobot.RegisterLicense('YourCompanyName', 'YOUR_FANUC_LICENSE_KEY');YaskawaRobot.RegisterLicense('YourCompanyName', 'YOUR_YASKAWA_LICENSE_KEY');% Each brand has its own license statefanucInfo = FanucRobot.LicenseInfo;yaskawaInfo = YaskawaRobot.LicenseInfo;disp(char(fanucInfo.ToString()));disp(char(yaskawaInfo.ToString()));% Check the brands of your cell once at startupif ~fanucInfo.IsLicensederror(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
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 onesposition = 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 vectorjoints = robot.Motion.ReadJoints(int32(1));disp(double(joints.Values));robot.Disconnect();
ReadJoints is not available on every brand: see 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.
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 answeredif robot.State.IsAvailable(StateFeature.Read)state = robot.State.Read();disp(char(state.Mode.ToString()));endtryrobot.State.Read();catch e% ExceptionObject is the .NET exception, for example a FeatureNotSupportedExceptionif isa(e, 'NET.NetException')disp(char(e.ExceptionObject.Message));elserethrow(e);endendrobot.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.NetExceptionin Matlab. ItsExceptionObjectproperty holds the .NET exception (RobotControllerException,FeatureNotSupportedException...), with itsMessageand its properties. - The methods with a progress callback, like
ReadBytesWithProgressofrobot.File, need a .NET delegate. Prefer the methods without a callback, likeReadBytes. RobotControllerhas no event. The objects of the brand SDKs, given byrobot.Native, have some: a .NET event needsaddlistener, 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, .NET methods and .NET enumerations.
What to read next
- Connect to your robot: the connection parameters of each brand and the settings of the controllers.
- Work without a robot: the simulators of the robot makers.
- Common API: the services of
RobotController. - Routing: which protocol each method uses on each brand.
- Licensing: the trial and the license key of each brand.