Questions tagged [matlab-struct]

For questions about the MATLAB structure datatype.

A structure is a datatype in MATLAB. It is different from cells and arrays in that it uses field names, and within fields one can store other data, also of type struct, cell, array, etc.

MathWorks has a tutorial on accessing data in a structure array.

The basic function to create one is, among others, struct.

145 questions
4
votes
1 answer

Can regexp return key/value pairs in a structure?

Let's say I have a character array with key/value pairs: ch = sprintf('name: John\nsex: M\n') ch = 'name: John sex: M ' This is just a sample. The actual data is in a file and has many pairs. I can use regexp to get tokens, then use a…
Xiangrui Li
  • 2,386
  • 2
  • 13
  • 17
4
votes
3 answers

How do I rename a field in a structure array in MATLAB?

Given a structure array, how do I rename a field? For example, given the following, how do I change "bar" to "baz". clear a(1).foo = 1; a(1).bar = 'one'; a(2).foo = 2; a(2).bar = 'two'; a(3).foo = 3; a(3).bar = 'three'; disp(a) What is the best…
Matthew Simoneau
  • 6,199
  • 6
  • 35
  • 46
4
votes
1 answer

MATLAB: Determine total length/size of a structure array with fields as structure arrays

I have a structure array containing fields as structure arrays of varying length. For example: 's' is a structure 'data' is a field in 's', and also a structure array itself and length(s(n).data) ~= length(s(m).data) I want to preallocate an array…
Doresoom
  • 7,398
  • 14
  • 47
  • 61
4
votes
3 answers

Matlab: Can I refer to array indices via unique names?

Suppose I have a column matrix pols containing vectors of [theta, rho, z]. Which means, if I have 9 such vectors, it will be a 9x3 matrix. It is quite handy to have them arranged as such, because I can just feed any one of them to functions like…
syockit
  • 5,747
  • 1
  • 24
  • 33
3
votes
2 answers

How can I count the number of properties in a structure in MATLAB?

I have a function that returns one or more variables, but as it changes (depending on whether the function is successful or not), the following does NOT work: [resultA, resultB, resultC, resultD, resultE, resultF] = func(somevars); This will…
Christian P.
  • 4,784
  • 7
  • 53
  • 70
3
votes
3 answers

Size of struct within another struct in Matlab (R2010a 64-bit linux)

I'm working with a Matlab API that loads data from a proprietary format into a series of structures. Here's an example of what a dataset looks like after loading a file: >>fieldnames(data(1)) ans…
jpatton
  • 394
  • 3
  • 17
3
votes
1 answer

Convert struct array to cell of structs and vice versa

I often have to work with struct arrays and cells containing scalar structs with identical fieldnames which I call an "unpacked struct array" and I wonder if there aren't already functions in Matlab and/or GNU Octave which helps converting between…
Andy
  • 7,931
  • 4
  • 25
  • 45
3
votes
2 answers

Can the values of this structure be accessed/modified without using a for loop?

Here’s my initial structure array: A(1).B.C = 'a'; A(2).B.C = 'b'; A(3).B.C = 'a'; A(4).B.C = 'a'; I want to change the values of field C based on values of Values and indexes of IndexingArray: Values = {'a', 'b'}; IndexingArray = [1 1 0 1]; So,…
3
votes
2 answers

How to extract all the values from a specific field of a n*1 structure in Matlab

Assume I have a structure called I of the size n*1, with a multiple fields one of which is called 'area' for example. When I try the code below: area = I.area the resultant area only have the one value that comes from the last position of the…
jwm
  • 4,832
  • 10
  • 46
  • 78
3
votes
1 answer

How can I perform a union with two single-field Matlab structs

I currently have two single-field Matlab structs that list image names. I would like to combine them into a single struct with no duplicates - i.e. a(1).img = 'aa.jpg' a(2).img = 'bb.jpg' b(1).img = 'bb.jpg' b(2).img = 'cc.jpg' I would like for ab…
user1245262
  • 6,968
  • 8
  • 50
  • 77
3
votes
2 answers

Equality of structure objects

I have a tree structure in matlab like this: node = sub: [1x4 struct] where each sub is also a node. node.sub(1) = sub: [1x4 struct] etc. with leaf nodes having empty sub. Suppose I have a node and I am traversing down the tree. Is there…
TyanTowers
  • 160
  • 10
3
votes
1 answer

Convert struct to double type in Matlab

Glcm (feature extraction method) give me an output in 'struct' type, while i need the output in 'double' type. I need a 'double' type as the input variable for the next step. So, I've tried to convert it using several code showed below. [gl] = glcm…
Ana Ain
  • 173
  • 1
  • 3
  • 14
3
votes
2 answers

How can I merge structures/structure arrays in MATLAB?

I am trying to merge two structs with identical fields. I tried several ways, such as this and this. But it either turns out sideways or doesn't work at all. My two (simplified) structs are a(1).name = 'x'; a(1).data = 1; a(2).name = 'y'; a(2).data…
Arthur Tarasov
  • 3,517
  • 9
  • 45
  • 57
3
votes
2 answers

How can I make this text file into a list in MATLAB?

I have a text file and would like to import it into MATLAB and make it a list: Person1 name = steven grade = 11 age= 17 Person2 name = mike grade = 9 age= 15 Person3 name = taylor grade = 11 age= 17 There are a few hundred entries like these…
Ben Fossen
  • 1,331
  • 6
  • 21
  • 33
2
votes
1 answer

Is there a way to iterate through nested structs with a varying number of fields in MATLAB?

I am looking for a way to iterate through nested structs in MATLAB, with each sub-struct containing a varying number of fields, some of which may contain further nested structs. For example, I have the following struct A: A.a.alpha=[2,3]; …
METCOMechE
  • 23
  • 4
1
2
3
9 10