Readtable to put text into columns from .CSV file

조회 수: 8(최근 30일)
I have 180 .CSV files that I am wanting to export into .txt files. all my data is in one column, seperated by a comma and I want to put them in three different columsn in the text file. With the code below, I am able to make a text file, but each line is seperated by a ' " ', which is messing up loading the text file. How do I edit the readtable for it to read the table correctly in the text file?
To make the files .txt files I used:
clear all
s = dir('*.CSV');
names = {};
for n = 1:numel(names)
Data = readtable(names{n},'Delimiter', ',','Format', '%s%s%s');
if ~isempty(k)
filename = extractBefore(filename,'-');
writetable(Data,[filename '.txt']);
% [file,path] = uigetfile('DOM*.xlsx','Select File to Plot','MultiSelect','on');
% fbfile = fullfile(path,file);
  댓글 수: 3
Kacey Lange
Kacey Lange 2022년 7월 11일
This is what I want it to look like

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

채택된 답변

Debadipto 2022년 7월 11일
As per my understanding, you have a code snippet that converts contents in a .csv file into a .txt file. Since the .csv files have both ',' and '"' as two conflicting delimiters, you're not able to produce the desired output. To get the desired .txt output, just remove the optional arguments from the readtable method.
Data = readtable(names{n});
Hope this helps.
  댓글 수: 7
Kacey Lange
Kacey Lange 2022년 7월 11일
Yes this worked! Thank you so much!

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

추가 답변(1개)

dpb 2022년 7월 11일
Not at all clear why you would want a quoted string where the quotes are over the whole line instead of fields, but that's a fairly simple exercise -- although would be trivial if you were with R2020b which introduced readlines -- with R2019a, you've got just a little more work --
data=strrep(data,char([13 10]),';');
You've got to strip the newline character pair to avoid putting them explicitly in the data instead of being part of the file structure using fileread -- it returns the entire content of the file.
Later, the readlines function returns a string array directly; you could also read the file and echo records with fgetl, but that's also a little more coding effort to open/close the file handles, write the explicit loop, etc., ...

Community Treasure Hunt

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

Start Hunting!

Translated by