How to store some values ​​in a txt file from a loop?

1 次查看(过去 30 天)
file = dir ('*.dat')
for i = 1: lenght(file);
data = fopen(file(i).name);
soil = fread(data,[1000 1000], 'float32');
teste = find(soil<0);
soil(teste) = NaN;
soil_end = soil/6;
save (soil_end)
end
I have an .m file that opens binary data and extracts soil information from it and divides the values ​​found by 6. I would like to store the value of each loop interaction. However, after each loop, the previous value is overwritten, is there a way I can save them all in a .txt file?
Many thanks in advance for your help!
  4 个评论
matquest
matquest 2020-3-31
You can add a couple of lines to your code to save the values:
fid = fopen('saved_data.txt', 'w'); % open the output .txt file for writing
file = dir('*.dat');
for ii = 1:length(file)
data = fopen(file(ii).name);
soil = fread(data, [1000 1000], 'float32');
fprintf(fid, '%f\n', soil); % this will save the value in the soil variable to the .txt file
...
end
fclose(fid) % close the output .txt file
Also note that matlab uses the values i and j as imaginary numbers, so they recommend that you use something else for your index (you'll often see ii or idx used instead).

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Agriculture 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by