How to save data from a for loop?

Hello everyone ,
i will need your help. I am running a consequence of files with a for loop, which matlab is reading and making some calculations with their data. What i want to do is to save certain results from the calculations of every file that is running (from every file thas is going through the loop). I need to do that in xlsx or txt file. Thank you in advance!
p.s i managed to succeed in saving the graphics from every file that i plotted:
savefig (h1, sprintf('Experimental_drying_rates_%s.fig ', num2str(i)));
savefig (h2, sprintf('Evaporation_coefficent_beta_%s.fig ', num2str(i)));
savefig (h3, sprintf('Calculated_drying_rates_%s.fig ', num2str(i)));

5 个评论

Note that rather than using num2str and sprintf like this:
sprintf('Experimental_drying_rates_%s.fig ', num2str(i))
you can use sprintf directly to convert the numeric value:
sprintf('Experimental_drying_rates_%d.fig ', i)
% ^^ numeric format specifier!
"I need to do that in xlsx or txt file."
okay, thank you, got it. But still i can't find a way to save the variables i need after reading each file from the loop. I got six files that matlab is reading in order and after finishing calculations of every file i need to save a few variables into txt files (to every file that is read should attach a txt file with results).
i am trying with something like this
for i=1:N
R=table (Time, beta, Sh, K, 'VariableNames' , {'Time', 'beta', 'Sh', 'K'});
writetable(R,'Results_%d.txt',i);
end
but it gives me error: Wrong number of arguments.
writetable is not sprintf. Do not mix them up.
To generate the filename, use sprintf as I showed you:
F = sprintf('Results_%d.txt',i);
Then use that filename in writetable:
writetable(R,F)
Thank you very much, it worked! i just don't know how to accept your answer :/

请先登录,再进行评论。

 采纳的回答

Where T is your table:
for k = ...
T = your table
F = sprintf('Results_%d.txt',k);
writetable(T,F)
end

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by