Convert .txt files to .CSV files

16 ビュー (過去 30 日間)
Curious Mind
Curious Mind 2018 年 10 月 2 日
回答済み: Walter Roberson 2018 年 10 月 3 日
Hi Guys:
I want help in converting .txt files in my current matlab directory into .csv files while retaining the original names of the files. Most importantly, The code should also save the .csv files into the same folder or directory as the .txt files. I really appreciate it.
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 10 月 2 日
Is this just renaming them or does data have to be extracted from the files and written in the csv?
Curious Mind
Curious Mind 2018 年 10 月 2 日
They need to be extracted and then saved with the .csv extension.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 10 月 3 日
dinfo = dir('*.txt');
filenames = {dinfo.name};
for K = 1 : length(filenames)
thisfile = filenames{K};
[basedir, basename, ~] = fileparts(thisfile);
newfile = fullfile(basedir, [basename '.csv']);
convert_to_csv(thisfile, newfile);
end
Where convert_to_csv is a function that you would write that would take a particular input file name and a particular output file name and do whatever is necessary to extract the data from that .txt and write it to the csv.
You have not told us anything about the structure of the .txt so we do not know what needs to be done. Some .txt files are very easy to convert to csv, but other .txt files can take a lot of work to convert to .csv

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by