How to save mat file using save() with a different variable name using for loop
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
a=[1,2,3,4,5,6,7,8,9];
b=[m,n,o,p,q,r,s,t,u];
for i=1:9
save('2_back_gp_1_trial_ str2num(a(i))_sub_1.mat',' b(i)')
end
0 Kommentare
Antworten (1)
Stephen23
am 6 Mär. 2022
Bearbeitet: Stephen23
am 6 Mär. 2022
"How to save mat file using save() with a different variable name using for loop"
That rather poor data design that seems to be tempting the use of dynamic variable names:
It could be done by dynamically naming a structure field and then using the -struct option, e.g.:
V = 1:9;
C = 'm':'u';
for k = 1:numel(V)
b = V(k);
S = struct(C(k),b);
F = fprintf('file_%d.mat',b);
save(F,'-struct','S')
end
However your code would be simpler and more efficient if you simply used exactly the same variable name in every file, e.g.:
V = 1:9;
for k = 1:numel(V)
b = V(k);
F = fprintf('file_%d.mat',b);
save(F,'b')
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!