how to add new column in existing text file?
조회 수: 14 (최근 30일)
이전 댓글 표시
I want to add new column in text files as their file names. I have 12000 files and i want to update them with their numbers. kindly help me to write a code for them. The desired text files should be as shown
![ss.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/262595/ss.jpeg)
댓글 수: 2
Rik
2020년 1월 20일
Ah, so you did post it as a separate question. Same question here: what have you tried so far? Try splitting a problem into solvable parts.
채택된 답변
Bhaskar R
2020년 1월 20일
편집: Bhaskar R
2020년 1월 20일
for ii = 1:12000
filename = [num2str(ii), '.txt']; % your file names as 1, 2.. .txt
mat = readmatrix(filename); % get file data
mat = [repmat(ii, size(mat, 1), 1), mat]; % append filename value to data
writematrix(mat, filename, 'Delimiter',' '); % write file with name
end
Hope this works for you !!
댓글 수: 7
Walter Roberson
2023년 11월 9일
filename = 'temp_summary.05.08_2011.txt';
%more general form
[~, basename] = fileparts(filename);
nameparts = regexp(basename, '\.', 'split');
dateparts = regexp(nameparts{end}, '_', 'split');
year_str = dateparts{end}
%special case where the year is the last thing after _
[~, basename] = fileparts(filename);
dateparts = regexp(basename, '_', 'split');
year_str = dateparts{end}
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Search Path에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!