1

I currently have a struct of arrays:

t2 = 

cotrain_idx: [7x1 double]
 trvate_idx: [7x1 double]
 conf_param: 0
  is_target: 0
   orig_idx: 0

I also have an struct array with fields:

t3 = 

1x7 struct array with fields:

    type
    latency
    urevent
    ImageName
    isTarget
    isAccurate
    origEventIdx

I would like to copy all the elements comprising t3.isTarget to t2.is_target. However all my attempts result in errors:

t2.is_target(:)=t3(:).isTarget;

seems to onlt copy the first element, even though t3(:).isTarget clearly returns all 7 elements:

>>> t3(:).isTarget

ans =

     0


ans =

     1


ans =

     1


ans =

     1


ans =

     1


ans =

     1


ans =

     1

Is there something obvious that I'm missing here? If I have to, I can copy the elements in one at a time, but I expect Matlab has a more efficient way to do this.

1 Answer 1

1

You can try the following, with a simplified version of you strcture:

t2.a1=123

t3(1).isTarget=1
t3(2).isTarget=2
t3(3).isTarget=3

t2.isTarget=[t3.isTarget]

You ca access to all the element of the t3.isTarget field by using []

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm. Thanks. Is there an intuitive way to understand why this is the correct answer?
The [] work as array concatenation operator. They get the single values returned by the instruction t3.isTarget (which lists the element of the field isTarget of each element of the t3 array) and build the array isTarget in the t2 struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.