"Assignment between unlike types is not allowed" in STRUCTURES

조회 수: 30 (최근 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