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

1 回表示 (過去 30 日間)
hany
hany 2017 年 10 月 18 日
回答済み: Himanshu 2024 年 12 月 10 日
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

回答 (1 件)

Himanshu
Himanshu 2024 年 12 月 10 日
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.

カテゴリ

Help Center および File ExchangeMATLAB Report Generator についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by