Saving Output of loop iterations in a file (mat, CSV).

3 visualizzazioni (ultimi 30 giorni)
hany
hany il 18 Ott 2017
Risposto: Himanshu il 10 Dic 2024
Hi I want to save the output of three nested for loops in a mat file or CSV so as to be used later in processing with neural network. the code is as follow;
for z=1:1:20
for y=1:1:20
for x=1:1:20
c=readfis('myfile');
out=evalfis([x y z],c);
data=[x y z out];
save('output.mat',data);
end
end
end
so I want to save the output of each iteration in a separate row in the output mat file.
Thanks

Risposte (1)

Himanshu
Himanshu il 10 Dic 2024
Hi Hany,
To save the output of the nested loops in a mat/csv file, you would have to accumulate the results in an array or table and then save that array or table after the loops have completed. This is how you can do it for the .mat file:
% Initialize an empty array to store results
results = [];
for z = 1:20
for y = 1:20
for x = 1:20
c = readfis('myfile');
out = evalfis([x y z], c);
% Append the result of this iteration to the results array
results = [results; x, y, z, out];
end
end
end
% Save the results array to a .mat file
save('output.mat', 'results');
A similar approach can be taken for csv file as well. In that case, you would have to convert the array to a table first, and then write the table to a csv file.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by