このような場合、私はループを最初に実行したときに完全なメモリ割り当てを強制するので、これを逆に反復するのが好きです。その後、コードは次のようになります:1つのフレームが次に依存し、またはあなたは、総枠があるでしょうどのように多くの知らない
%Reset the structure
s = struct;
for ix = 97:-1:1
%Do stuff
%Store the data
s(ix).frame = ix;
s(ix).str = strength;
s(ix).freq = frequency;
end
場合は、前方にスキャンすることができます。 97フレームは多くのデータではないので、問題の事前割り当て部分を最適化することについてあまり心配する必要はありません。
%Reset the structure
s = struct;
for ix = 1:97
%Do stuff
%Store the data
s(ix).frame = ix;
s(ix).str = strength;
s(ix).freq = frequency;
end
それとも、あなたが本当に構造の事前割り当てられた配列のパフォーマンスを必要とする場合がありますが、それは始まりになりますどのように大きな分からない、あなたはこのような何かを行うことができます。
%Reset the structure
s = struct;
for ix = 1:97
%Do stuff
%Extend if needed
if length(s)<ix
s(ix*2).frame = nan; %Double allocation every time you reach the end.
end
%Store the data
s(ix).frame = ix;
s(ix).str = strength;
s(ix).freq = frequency;
end
%Clip extra allocation
s = s(1:ix);
ああありがとうございます。あなたの追加情報もまた私にとって大いに役立つでしょう。ありがとう – saya