Hey Community,
I am trying to solve a problem where I want to assign a field within a structure to another structure that contains a cell array of structures. For example, lets say I have some structure, pet.dog.type that I want to use to create another structure. For example dog.name1 = pet.dog.type{1}.name, dog.name2 = pet.dog.type{2}.name. Is there a way to create a structure in this way? When I try to do this I keep getting an error "unable to perform assignment because dot indexing is not supported for variables of this type."

 채택된 답변

Image Analyst
Image Analyst 2019년 1월 25일

1 개 추천

When you say "pet.dog.type{1}.name," you're saying that one of the fields, "grand field" if you'd like rather than a child field, is actually a cell array. So if you did
ca = pet.dog.type{1};
you'd have a cell. ca would be a cell, NOT another structure. Cells cannot take fields. So you cannot do
v = ca.name;
because ca is a cell array and cell arrays do not take dot indexing - they have no field so they can't, it's impossible. So that's why you cannot say pet.dog.type{1}.name.

댓글 수: 5

Samuel Welker
Samuel Welker 2019년 1월 25일
Can you give me an alternative solution that would not require a for loop?
You don't need a loop. Just start assigning the variables but you need to make sure you reference them correctly.
An field of a structure can be anything - a scalar, an array, an image, another structure, a cell array, or whatever. I don't know what
  1. your pet structure array is since you didn't attach pet in a .mat file, and
  2. I don't know what you intend to do - what assignments should be made - since what code you gave doesn't make sense at all.
If you need further help we will need both of those things
save('pet_structure.mat', 'pet');
Samuel Welker
Samuel Welker 2019년 1월 25일
Here is what I want to do
Image Analyst
Image Analyst 2019년 1월 25일
Well, I don't know, maybe I was wrong. But I DO know that it would be a lot easier for people to help you if you attached the structure in a .mat file rather than a screenshot.
Stephen23
Stephen23 2019년 1월 25일
편집: Stephen23 2019년 1월 25일
"When you say "pet.dog.type{1}.name," you're saying that one of the fields, "grand field" if you'd like rather than a child field, is actually a cell array. So if you did"
ca = pet.dog.type{1};
"you'd have a cell. ca would be a cell, NOT another structure."
This bold assertion is not supported by the MATLAB documentation, which clearly states that curly braces refer to the content of the cell array, not to the cell array itself:
The content of that cell could certainly be a structure with a field:
>> pet.dog.type{1}.name = 123;
>> pet.dog.type{2}.name = 222;
>> ca = pet.dog.type{1};
>> ca.name
ans = 123
>> size(ca)
ans = 1 1
>> class(ca)
ans = struct
It is not clear why Image Analyst stated that the content of a cell array must be another cell array. I don't find anything in the documenation to support this, and testing that exact code indicates that the content can indeed be a structure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

태그

질문:

2019년 1월 24일

편집:

2019년 1월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by