Can I define a structure iteratively?

조회 수: 2 (최근 30일)
Ricardo  Gomez Uribe
Ricardo Gomez Uribe 2017년 11월 22일
댓글: Ricardo Gomez Uribe 2017년 11월 22일
I am hoping to redefine a structure within each for-loop. Say, given a 1000-by-4 real vector Xs and a 1000-by-4 logical B,
value_i={[]};
fields_global={[]};
values_global={[]};
for i=1:30
for j=1:4
A = Xs(:,j);
C = A.*(B(:,j)*1);
E=C(find(C));
value_i={value_i,E};
end
Structure_i=struct('field_i',value);
Structure_global=struct(fields_global,values_global,'field_i',value_i)
fields_global={fields_global,'field_i'}
values_global={values_global,value_i}
value={[]}
end
MATLAB doesn't seem to appreciate my approach. Is this at all feasable? Is there another way to store
  댓글 수: 1
Ricardo  Gomez Uribe
Ricardo Gomez Uribe 2017년 11월 22일
Is there another way to store, within each iteration, vectors of varying magnitude, and reference them in the output of a function as a structure?

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

답변 (1개)

Guillaume
Guillaume 2017년 11월 22일
It's not very clear what you're trying to achieve. As it is you're always creating the same field always called 'field_i'. You can't have multiple fields with the same name.
The simplest way to store several vectors of different length (if that's what you mean by magnitude) is to use a cell array.
results = cell(1, 30);
for i = 1:30
values = ...
results{i} = values;
end
You could use a structure with numbered fields instead but that would be more complicated for no benefit at all.
  댓글 수: 2
Ricardo  Gomez Uribe
Ricardo Gomez Uribe 2017년 11월 22일
That should do it. Cheers!
Ricardo  Gomez Uribe
Ricardo Gomez Uribe 2017년 11월 22일
One more question: can one code nested cells? That is, in your example,
results = cell(1, 30);
for i = 1:30
values = ...
results{i} = values;
end
could "value" be a cell array?

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

카테고리

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