How to pull an item and save it into an array/matrix from a Cell that is composed of a Struct

1 回表示 (過去 30 日間)
I have a myVar which is a 1x27 Cell, where each Cell is a Struct. Within the Struct is a Field called myPullItem that I want to grab. However 'pull' with a '(i)' afterwards to increment the array produces an error; when I remove the (i), it only provides me the last cell's Struct myPullItem
for i=1:length(myVar)
pull(i) = myVar{i}.myStruct.myPullItem
end
Error: In an assignment A(:) = B, the number of elements in A and B must be the same.
My objective is to save each field. I am attempting to do this by incrementing through each cell of Structs and saving it.
What am I doing wrong? Thx.

採用された回答

nas illmatic
nas illmatic 2015 年 10 月 18 日
I think I got it. Apparently you need to define 'pull' first. So, you need to define it as
pull=cell(1)
And then it works.

その他の回答 (2 件)

James Tursa
James Tursa 2015 年 10 月 18 日
編集済み: James Tursa 2015 年 10 月 18 日
pull(i) is likely a scalar, while myVar{i}.myStruct.myPullItem is likely not a scalar. You are trying to stuff multiple values into a scalar, hence the error.
What is myVar{i}.myStruct.myPullItem? Once we know the size we can offer suggestions (e.g., using another cell array, or perhaps an nD array, etc). E.g., something like this:
pull{i} = myVar{i}.myStruct.myPullItem;
or maybe this:
pull(:,:,i) = myVar{i}.myStruct.myPullItem;
Again, can't tell for sure what will work for you until we know what the rhs is.

nas illmatic
nas illmatic 2015 年 10 月 18 日
I tried
pull{i} = myVar{i}.myStruct.myPullItem;
but got Error: Cell contents assignment to a non-cell array object.
And,
pull(:,:,i) = myVar{i}.myStruct.myPullItem;
resulted in Error: Subscripted assignment dimension mismatch.
Types:
  • The myPullItem is a 1x16 char field
  • myStruct is a 1x1 struct with 1 field
  • myVar is a 1x27 cell where each cell is a struct

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by