フィルターのクリア

how to add new column in existing text file?

8 ビュー (過去 30 日間)
MS11
MS11 2020 年 1 月 20 日
編集済み: Jonathon Klepatzki 2023 年 11 月 9 日
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
  2 件のコメント
Rik
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.
MS11
MS11 2020 年 1 月 20 日
on the left is my input file and output file shown on the right. I add new column by using Excel but I have more than 12K files and I want to do the same with all of them with Matlab. can you write a code for this problem?

サインインしてコメントする。

採用された回答

Bhaskar R
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
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}
year_str = '2011'
%special case where the year is the last thing after _
[~, basename] = fileparts(filename);
dateparts = regexp(basename, '_', 'split');
year_str = dateparts{end}
year_str = '2011'
Jonathon Klepatzki
Jonathon Klepatzki 2023 年 11 月 9 日
編集済み: Jonathon Klepatzki 2023 年 11 月 9 日
Thank you, it worked!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by