1

I have defined an array of structs as follows:

t3(1:num_elems) = struct('prime_idx',0,'second_idx',0,);

I would like to copy an array nic to the prime_idx field of t3, so that t3(x).prime_idx == nic(x) for all x. I've tried:

 t3.prime_idx = nic;

and [t3.prime_idx] = nic; and t3.prime_idx = [nic]; and [t3.prime_idx] = [nic];

and various permutations of adding (:) after the two array variables t3 and nic.

How can I copy each element of nic to the corresponding prime_idx field of the corresponding element of t3?

1 Answer 1

1

The expression s.f, where s is a struct array, results in a comma separated list. Thus, writing s.f = x; is invalid, and [s.f] = x; would work, but requires x to return as many outputs as s has items. What you need is to provide those outputs, e.g. like this:

cNic = num2cell(nic);
[t3.prime_idx] = cNic{:};
Sign up to request clarification or add additional context in comments.

Comments

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.