필터 지우기
필터 지우기

array of struct arrays with varying length

조회 수: 13 (최근 30일)
Greg
Greg 2012년 5월 18일
Ideally, my calculation needs to preallocate an array, myArray, (of predetermined length) in which each element is an array of structs -- an array which may grow in length during the course of the calculation. These arrays of structs must grow independently, so at the end of the calculation, the array of structs in myArray(1) may have a different length than myArray(2), etc.
The memory need not be contiguous, neither for myArray nor for the array of structs myArray(i).
Trying this
myArray(1:10) = { [ struct('index',int32(0), 'result',[]) ] };
% ... later, when an element needs to grow:
myArray(1) = { myArray(1); [ struct('index', int32(0),'result',[]) ] };
fails on that last line with the message, "In an assignment A(:) = B, the number of elements in A and B must be the same."

채택된 답변

Walter Roberson
Walter Roberson 2012년 5월 18일
A cell array can hold a struct array in each location, but a struct array location cannot hold a struct array directly. A struct array indexed at an element must be a structure scalar. That structure scalar could have a single field which held a struct array, though.
myArray{1} = [myArray{1}; struct('index', int32(0),'result',[]) ];

추가 답변 (0개)

카테고리

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