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

조회 수: 1 (최근 30일)
pink flower
pink flower 2020년 3월 31일
댓글: pink flower 2020년 4월 1일
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).
pink flower
pink flower 2020년 4월 1일
Thank you very much!! It worked now!

댓글을 달려면 로그인하십시오.

답변 (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