0

Take an example,

a = struct('in',1,'out',2)
b = struct('temperature',6,'light',5,'output',2)

How do I get the last field and value that I input?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wizztjh
  • 6,979
  • 6
  • 58
  • 92

2 Answers2

5

To get the last field of a,

z = a.out

To get the last field of b,

x = b.output

Or if you don't know what the field names are you can find them in the following way:

names = fieldnames(a)

I hope that helps.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
matcheek
  • 4,887
  • 9
  • 42
  • 73
3

The documentation for a struct says:

The most common way to access the data in a structure is by specifying the name of the field that you want to reference.

In contrast to an array, where elements are accessed by index -> position, a struct field is accessed by the fieldname -> name.

See How do I access MATLAB structure fields within a loop? on how to iterate over all all fields of a struct.

Community
  • 1
  • 1
zellus
  • 9,617
  • 5
  • 39
  • 56