"Assignment between unlike types is not allowed" in STRUCTURES

60 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2019-6-6
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.

更多回答(1 个)

KSSV
KSSV 2019-6-6
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) ;

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by