clear all;
Estructures a MATLAB
En aquest tutorial explicarem què són les estructures de MATLAB i com treballar-hi.
Activeu "Output inline" a la dreta de la barra de desplaçament.

Crear una estructura simple
Una estructura és un tipus de dada que agrupa dades relacionades utilitzant camps amb nom.
En pots crear una mitjançant assignació directa o amb la funció struct.
robot.name = 'MyRobot';
robot.DOF = 6;
robot.payload = 2.5
robot = struct with fields:
name: 'MyRobot'
DOF: 6
payload: 2.5000
Creació equivalent amb struct()
robot2 = struct('name','MyRobot', ...
'DOF',6, ...
'payload',2.5)
robot2 = struct with fields:
name: 'MyRobot'
DOF: 6
payload: 2.5000
Estructures imbricades
Les estructures poden contenir altres estructures, permetent dades jeràrquiques.
robot2.links(1) = struct('length',0.5,'mass',4.0)
robot2 = struct with fields:
name: 'MyRobot'
DOF: 6
payload: 2.5000
links: [1x1 struct]
robot2.links(2) = struct('length',0.4,'mass',3.5)
robot2 = struct with fields:
name: 'MyRobot'
DOF: 6
payload: 2.5000
links: [1x2 struct]

Accedir als camps
S’accedeix als camps amb notació de punt: NomEstructura.NomCamp
Llegir un camp
robotName=robot.name
robotName = 'MyRobot'
Escriure/actualitzar un camp
robot.payload = 3.0
robot = struct with fields:
name: 'MyRobot'
DOF: 6
payload: 3
Accedir a un camp imbricat
link1_length = robot2.links(1).length
link1_length = 0.5000
robot2.links(2).length = 0.8;
Noms de camp dinàmics
Pots afegir o accedir a camps amb noms de variable utilitzant parèntesis.
newfield = 'maxSpeed';
robot.(newfield) = 1.2; % adds a new field maxSpeed
Comprovar l’existència abans d’accedir-hi
if isfield(robot, newfield)
RobotMaxSpeed = robot.maxSpeed
end
RobotMaxSpeed = 1.2000
Afegir i eliminar camps
Utilitza setfield i rmfield, o manipulació directa.
Afegir un camp
robot = setfield(robot, 'manufacturer', 'UniversalRobots')
robot = struct with fields:
name: 'MyRobot'
DOF: 6
payload: 3
maxSpeed: 1.2000
manufacturer: 'UniversalRobots'
Eliminar un camp
robot = rmfield(robot, 'manufacturer')
robot = struct with fields:
name: 'MyRobot'
DOF: 6
payload: 3
maxSpeed: 1.2000