Add a field to an existing structure

I have a structure called "Events", containing a field called "Efix" for each trial in my data. Efix contains a subfield called "duration" and I want to add a field that copies this duration data. I'd like the new field to be called "duration_copy".
I've tried lots of different iterations, some of which are:
>> Events.Efix.duration_copy = Events.Efix.duration;
>> Events.Efix(:).duration_copy = Events.Efix(:).duration;
>> Events.Efix.duration_copy = Events.Efix.duration{:};
For these, I receive the error: "Expected one output from a curly brace or dot indexing expression, but there were 6 results."
I also tried:
>> [Events.Efix(:).duration_copy] = Events.Efix.duration;
>> [Events.Efix(:).duration_copy] = Events.Efix(:).duration;
>> [Events.Efix(:).duration_copy] = Events.Efix{:}.duration;
For these, I receive the error: "Scalar structure required for this assignment."
What code would accomplish this?
Edited to add screenshots:Screen Shot 2019-01-08 at 1.21.21 PM.png
Screen Shot 2019-01-08 at 1.21.34 PM.png

Answers (1)

9 Comments

Hi Madhan,
I saw this answer before I posted and tried it with my own structure:
[Events.Efix.(duration_copy)] = Events.Efix.duration{:};
And received the error: "Undefined function or variable 'duration_copy'."
I also tried:
[Events.Efix.duration_copy] = Events.Efix.duration{:};
Received the error: "Scalar structure required for this assignment."
Is there an error in the way I've written my code?
[Events.Efix.duration_copy] = Events.Efix.duration;
Hi Walter,
I again get the error "Scalar structure required for this assignment."
Any idea what could be the problem and how to resolve it?
Edit: I should add, I tried just putting the "Events.Efix.duration" data into a test variable:
>> a = Events.Efix.duration;
And I get an error: "Expected one output from a curly brace or dot indexing expression, but there were 6 results."
Which MATLAB release are you using? In R2018b,
>> A.B = struct('address', {'abc', 'pqr', 'stu'})
A =
struct with fields:
B: [1×3 struct]
>> A.B.address
ans =
'abc'
ans =
'pqr'
ans =
'stu'
>> [A.B.address_copy] = A.B.address
A =
struct with fields:
B: [1×3 struct]
Hi Walter,
I'm using 2015b
I updated to 2018b, and get the same error: "Scalar structure required for this assignment."
I also tried to step through the same steps you show to compare outputs:
>> Events
Events =
1×6 struct array with fields:
Messages
Efix
Esacc
Eblink
But there's again an error when I try to duplicate your step below:
>> A.B.address
ans =
'abc'
ans =
'pqr'
ans =
'stu'
When I do it, I get:
>> Events.Efix.duration
Expected one output from a curly brace or dot indexing expression, but there were 6 results.
Is the issue that "Events" has other fields in addition to "Efix"? What am I missing here?
Screenshots:
Screen Shot 2019-01-09 at 11.48.21 AM.png
Screen Shot 2019-01-09 at 11.48.35 AM.png
The difference is that your top level structure is non-scalar, each entry of which contains a field Efix which all happen to be scalar struct (it looks like.) The test I show was for the case of a scalar top-level struct that had a non-scalar struct Efix inside it.
Indices for S and fields 1 through N-1 specify individual elements of structure arrays. Indices for field N specify one or more elements of the array in that field, which can be of any type.
is that you cannot do it one setfield call -- because in order to do it for nonscalar S your first index would have to be to specify something that was not an individual element of the array, which is not permitted.
So I think you are going to need to use a loop.
Ah! Ok, that makes some sense. I'm self-taught with matlab (and programming), and still learning about its intricaies. I couldn't put my finger on what the difference was until you explained it. Many thanks, Walter!
For anyone running into a similar problem, this worked for me:
for iTrial=1:6
Events(iTrial).Efix.duration_copy = Events(iTrial).Efix.duration;
end

Sign in to comment.

Categories

Tags

Asked:

on 8 Jan 2019

Commented:

on 9 Jan 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!