Saving Output of loop iterations in a file (mat, CSV).
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Himanshu
on 10 Dec 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.
0 Comments
See Also
Categories
Find more on MATLAB Report Generator 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!