How to save multiple files in a 'for' loop

5 ビュー (過去 30 日間)
yunwei hu
yunwei hu 2020 年 2 月 18 日
編集済み: Stephen23 2020 年 2 月 18 日
Hi everyone,
i am trying to save multiple files in a for loop.
however, the saved files of coordinate2 always inclueds the data from coordinate 1. 3 incluedes all data from 2 and 1.
i upload the first three files of coordinate to show the problems-
does anyone know how to save the data seperately in different files?
coordinate= [];
i =1:6;
for t=1:length(i)
load(['snakes_frame' num2str(t) '.mat']);
for a=1:length(snakes)
X = snakes{1,a}.x;
Y = snakes{1,a}.y;
coordinate=[coordinate,X, Y];
end
save(['C:\Users\length\coordinate' num2str(t) '.mat'],'coordinate');
end

採用された回答

Stephen23
Stephen23 2020 年 2 月 18 日
編集済み: Stephen23 2020 年 2 月 18 日
Move the coordinate=[] line inside the first loop:
for t = 1:numel(i)
coordinate = [];
... the rest of your code
end
Even better would be to get rid of the inner loop. If you had saved the data in non-scalar structure this would be trivial, but because you appear to have scalar structure in a cell array it is more complex, but something like this should work:
P = 'C:\Users\length';
V = 1:6;
for kk = 1:numel(V)
F = sprintf('snakes_frame%u.mat',V(kk)); % better than NUM2STR.
S = load(F);
S = [S.snakes{:}]; % convert ugly cell-of-scalar-structures to one non-scalar structure.
coordinate = reshape([S.x;s.y],1,[]);
G = sprintf('coordinate%u.mat',kk);
save(fullfile(P,G),'coordinate');
end
Read more:
  1 件のコメント
yunwei hu
yunwei hu 2020 年 2 月 18 日
Thanks! it works.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by