필터 지우기
필터 지우기

How to adjust and save a .txt file within a for loop?

조회 수: 5 (최근 30일)
Klaas
Klaas 2017년 2월 8일
댓글: Jan 2017년 10월 28일
I have a .dpj file which is in fact a normal .txt file. I would like to open, change a number, save under a specific name and close the file all within a for loop without loosing the basic file.
My question is how I can do this for example if the number is standing in the line 100 of the text file behind KLEFF?
The filename under which I want to save the .dpj file should be numbered depending on the for loop.
By myself this part works except that the .dpj files are not readable anymore:
N=10;
for i=0:N;
baseline_name='textfile';
filename=[baseline_name,'_',num2str(i)];
savename=strcat(char(filename),'.dpj');
save (savename);
end
Many thanks in advance!
  댓글 수: 1
Rik
Rik 2017년 2월 9일
Aren't you saving an empty file with this command?

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

채택된 답변

Jan
Jan 2017년 2월 9일
Folder = cd; % Set accordingly
N = 10;
for iFile = 0:N
% Read the text:
File = fullfile(Folder, sprintf('textfile%d.dpj', iFile));
Str = fileread(File);
CStr = strsplit(Str, '\n');
% Change the line:
Line = CStr{100};
Line = [strtok(Line, '='), sprintf('= %g', rand)];
CStr{100} = Line;
% Write the text:
NewFile = fullfile(Folder, sprintf('textfile%d_modified.dpj', iFile));
fid = fopen(NewFile, 'w');
if fid == -1
error('Cannot open file for writing: %s', FileName);
end
fprintf(fid, '%s\n', CStr{:});
fclose(fid);
end

추가 답변 (1개)

Nagi Aly
Nagi Aly 2017년 10월 27일
Hello? I have a dataset in folder names 'train'. In train we have some folders (set00, .... set05), in each set we have some videos like V000, VOO1 ... and in each video we have some text files which contains data. I would like to go in and save all the columns 4 in eatch file into another fileText.I try with this code but I can not save the contents of the files in another file. If you can help me please
  댓글 수: 1
Jan
Jan 2017년 10월 28일
Please open a new thread for a new question. Then post your code and explain what this means: "I can not save the contents of the files in another file".

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

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by