フィルターのクリア

Storing data from FOR loops

1 回表示 (過去 30 日間)
Sebastian Ciuban
Sebastian Ciuban 2014 年 9 月 4 日
コメント済み: Sebastian Ciuban 2014 年 9 月 5 日
Good evening,
I have encountered a problem regarding data storing from FOR loops. I'm using the following code:
[~,m]=size(obs);
for j=1:m;
[~,n]=size(obs(j).data);
for i=1:n
A=extr(eph,obs(j).data(1,i));
B(i).data=A;
end
end
% obs is a 1x2880 structure
% eph is a 36x212 matrix
% extr(eph,obs.data) is a function
% I want to store all values of B(i) in a new structure (1x2880)
The problem is that my code remembers the previous stored data and adds the new data in the current structure..How I can manage this situation?

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 9 月 4 日
Ciuban - you somehow need to use your j when updating B so that you don't overwrite previously stored data with the data from the current iteration. You could try creating a cell array of the structures as
[~,m]=size(obs);
% create an array for the structs
allData = cell(1,m);
for j=1:m;
[~,n]=size(obs(j).data);
% size B appropriately
B = repmat(struct('data',[]),1,n);
for i=1:n
A=extr(eph,obs(j).data(1,i));
B(i).data=A;
end
% assign B to allData
allData{j} = B;
end
At each iteration, the code "resets" B given the number of columns in the jth observation data vector. At the end of the inner for loop, we just assign B to the jth element of the allData cell array. Once completed, allData is a 1x2880 cell array of structures.
  1 件のコメント
Sebastian Ciuban
Sebastian Ciuban 2014 年 9 月 5 日
Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by