"Assignment between unlike types is not allowed" in STRUCTURES

56 views (last 30 days)
Hello,
Please suppose the following matrices and structure:
X1 = zeros(1, 4);
X2 = zeros(2, 4);
X3 = zeros(3, 4);
AA.SM = []; p = repmat(AA, 3, 1); % 3 is not fixed
PN.SM = p; PN = repmat(PN, 100, 1); % 100 is not fixed
When I want to assign X1, X2 and X3 to PN, I receive an error stating that "Assignment between unlike types is not allowed."
PN(1).SM(1) = X1;
PN(1).SM(2) = X2;
PN(1).SM(3) = X3;
I have also tried "setfield" function, but it does not work.
Could you please help me to resolve this difficulty?
Best regards,
AM

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jun 2019
X1 = zeros(1, 4);
so X1 is numeric
AA.SM = []; p = repmat(AA, 3, 1); % 3 is not fixed
so p is a 3 x 1 structure array, each element of which is a struct with field SM
PN.SM = p;
So PN is a scalar stucture array with field PN, and PN.SM is a 3 x 1 structure array, each element of which is a struct with field SM
PN = repmat(PN, 100, 1); % 100 is not fixed
and now PN is a 100 x 1 structure array, each entry of which has a field named SM, and each of those contains a 3 x 1 structure array, each element of which is a struct with field SM
PN(1).SM(1) = X1;
PN(1) is a single struct array entry with field SM that is a 3 x 1 struct array. PN(1).SM(1) is a single struct array entry, the contents of which is a struct with field SM. And you are trying to assign zeros(1,4) to it instead of a struct.

More Answers (1)

KSSV
KSSV on 6 Jun 2019
You amy try something like this:
PN = struct ;
PN(1).SM(1).X = rand(10,1) ;
PN(1).SM(2).X = rand(20,1) ;
PN(1).SM(3).X = rand(30,1) ;

Categories

Find more on Structures in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!