How to save multiple files in a 'for' loop
5 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Stephen23
on 18 Feb 2020
Edited: Stephen23
on 18 Feb 2020
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:
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!