How to save multiple files in a 'for' loop

4 次查看(过去 30 天)
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:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by