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
77
votes
4 answers

Iterating through struct fieldnames in MATLAB

My question is easily summarized as: "Why does the following not work?" teststruct = struct('a',3,'b',5,'c',9) fields = fieldnames(teststruct) for i=1:numel(fields) fields(i) teststruct.(fields(i)) end output: ans = 'a' ??? Argument to…
noio
  • 5,744
  • 7
  • 44
  • 61
35
votes
6 answers

How do I access structure fields dynamically?

I have a structure with many fields which are vectors of different lengths. I would like to access the fields within a loop, in order. I tried getfield as follows but MATLAB doesn't like that. How can I do this? S = struct('A', [1 2], 'B',[3 4…
marciovm
  • 613
  • 1
  • 5
  • 9
10
votes
2 answers

Why is MATLAB sensitive to order of fields in a struct array assignment?

First I specify A as a struct, and two other structs: B having the same order of elements, and C having a different order of elements. A.x = 11; A.y = 11; B.x = 21; B.y = 22; C.y = 31; %// Note that I am specifying C.x = 32; %// y first and x…
MGA
  • 1,658
  • 15
  • 28
10
votes
2 answers

How to sort structure arrays in MATLAB?

I'm working with an image retrieval system using color histogram intersection in MATLAB. This method gives me the following data: a real number which represents the histogram intersection distance, and the image file name. Because they are different…
zenab
9
votes
4 answers

How can I format strings for use as structure field names in MATLAB?

I want to remove hyphens (-), slashes (/) and white space () from a string name(i) so that I can use it as a structure field name. This is the ugly way I am currently doing it using the function strrep: cell2mat(strrep(strrep(strrep(name(i),…
Elpezmuerto
  • 5,391
  • 20
  • 65
  • 79
8
votes
1 answer

How can I dynamically access a field of a field of a structure in MATLAB?

I'm interested in the general problem of accessing a field which may be buried an arbitrary number of levels deep in a containing structure. A concrete example using two levels is below. Say I have a structure toplevel, which I define from the…
Marc
  • 5,315
  • 5
  • 30
  • 36
7
votes
4 answers

MATLAB "bug" (or really weird behavior) with structs and empty cell arrays

I have no idea what's going on here. I'm using R2006b. Any chance someone out there with a newer version could test to see if they get the same behavior, before I file a bug report? code: (bug1.m) function bug1 S =…
Jason S
  • 184,598
  • 164
  • 608
  • 970
7
votes
1 answer

access struct data (matlab)

a= struct('a1',{1,2,3},'a2',{4,5,6}) how can Iget the value of 1; I try to use a.a1{1} which return errors >> a.a1{1} ??? Field reference for multiple structure elements that is followed by more reference blocks is an error. How can I access 1?…
Sean
  • 4,267
  • 10
  • 36
  • 50
7
votes
3 answers

Difference between empty Matlab struct S and all elements S(:)

My question is: What is the difference between S and S(:) if S is an empty struct. I believe that there is a difference because of this question: Adding a field to an empty struct Minimal illustrative example: S = struct(); %Create a struct S(1) =…
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
6
votes
2 answers

Acessing multiple structure fields in matlab without looping through it

I Have a 8x18 structure with each cel containing a column vector of occurrences of a single event. I want to obtain data from some of these fields concatenated in a single array, without having to loop through it. I can't seem to find a way to…
Jasper
  • 75
  • 5
6
votes
1 answer

Efficient indexing of structures in MATLAB

Up until recently, I have been storing time series data in structs in MATLAB by placing the index after the field name, e.g.: Structure.fieldA(1) = 23423 So, the struct has a set of fields, and each field is a vector. I've seen a lot of other…
CaptainProg
  • 5,610
  • 23
  • 71
  • 116
5
votes
3 answers

How can I preallocate a non-numeric vector in MATLAB?

I've often found myself doing something like this: unprocessedData = fetchData(); % returns a vector of structs or objects processedData = []; % will be full of structs or objects for dataIdx = 1 : length(unprocessedData) …
Dan Vinton
  • 26,401
  • 9
  • 37
  • 79
5
votes
1 answer

How to load a MATLAB struct into a R data frame?

I have a MATLAB struct, containing a number of fields which together describe, say, 100 observations of a number of variables, as follows (MATLAB output): mystruct = fieldA: [100x1 double] fieldB: [100x1 double] fieldC: [100x1 double] …
Matt Mizumi
  • 1,193
  • 1
  • 11
  • 27
4
votes
1 answer

How can I display and access structure array contents in MATLAB?

Firstly, I have the user input their own text files consisting of states, capitals, and populations and I put all of these values into a structure array using the following code: clear clc %Part A textfile=input('What is the name of your text…
Nick
  • 85
  • 2
  • 3
  • 5
4
votes
2 answers

MATLAB structure merge

I have the following struct data = id: [143x1 double] datenum: [143x1 double] Timestamp: {143x1 cell} Min_F1_USA_40__u: [143x1 double] Max_F1_USA_40__u: [143x1 double] …
Mokus
  • 10,174
  • 18
  • 80
  • 122
1
2 3
9 10