Readtable to put text into columns from .CSV file

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:
%% CDOM
clear all
s = dir('*.CSV');
names = {s.name};
for n = 1:numel(names)
Data = readtable(names{n},'Delimiter', ',','Format', '%s%s%s');
[fullpath,filename,ext]=fileparts(names{n});
filename=strrep(filename,'-','.');
k=strfind(filename,'-');
if ~isempty(k)
filename = extractBefore(filename,'-');
end
writetable(Data,[filename '.txt']);
end
% [file,path] = uigetfile('DOM*.xlsx','Select File to Plot','MultiSelect','on');
% fbfile = fullfile(path,file);

3 comentarios

dpb
dpb el 8 de Jul. de 2022
You're going to have to show us what you actually have you're starting from and then what it is, specifically, you want for an output file format.
Kacey Lange
Kacey Lange el 11 de Jul. de 2022
The .CSV file is what I am starting with and the .txt file is what I got
Kacey Lange
Kacey Lange el 11 de Jul. de 2022
This is what I want it to look like

Iniciar sesión para comentar.

 Respuesta aceptada

Debadipto
Debadipto el 11 de Jul. de 2022

0 votos

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 comentarios

Kacey Lange
Kacey Lange el 11 de Jul. de 2022
I get this error:
Error using readtable (line 216)
Cannot interpret data in the file 'DOM01-1.CSV'. Found 2 variable names but 1 data columns. You may need to specify a
different format, delimiter, or number of header lines.
Note: readtable detected the following parameters:
'Delimiter', ',', 'HeaderLines', 0, 'ReadVariableNames', true, 'Format', '%q'
Error in FDOM_rename (line 26)
Data = readtable(names{n});
Debadipto
Debadipto el 11 de Jul. de 2022
Editada: Debadipto el 11 de Jul. de 2022
Please let me know if you're able to run the following code snippet at your end without any errors:
clear all
s = dir('DOM01-1.CSV');
names = {s.name};
for n = 1:numel(names)
Data = readtable(names{n});
[fullpath,filename,ext]=fileparts(names{n});
filename=strrep(filename,'-','.');
k=strfind(filename,'-');
if ~isempty(k)
filename = extractBefore(filename,'-');
end
writetable(Data,[filename '.txt']);
end
Kacey Lange
Kacey Lange el 11 de Jul. de 2022
No, I get the same error
Debadipto
Debadipto el 11 de Jul. de 2022
Editada: Debadipto el 11 de Jul. de 2022
Which version of MATLAB are you using? I am currently using R2022a.
Kacey Lange
Kacey Lange el 11 de Jul. de 2022
R2019a
Debadipto
Debadipto el 11 de Jul. de 2022
The .csv file is present in UTF-16LE format, which is not supported by MATLAB 2019a apparently. So you'll have to type cast the data to UTF-8.
clear all
s = dir('DOM01-1.CSV');
names = {s.name};
for n = 1:numel(names)
fid = fopen(names{n}, 'rt', 'n', 'UTF16LE');
fread(fid, 2, '*uint8');
inputdata= fread(fid, [1 inf], '*char');
[fullpath,filename,ext]=fileparts(names{n});
filename=strrep(filename,'-','.');
k=strfind(filename,'-');
if ~isempty(k)
filename = extractBefore(filename,'-');
end
outputfile = fopen([filename '.txt'],'w');
fprintf(outputfile, inputdata);
fclose(outputfile);
end
Refer to this answer to know more.
Kacey Lange
Kacey Lange el 11 de Jul. de 2022
Yes this worked! Thank you so much!

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 11 de Jul. de 2022
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=fileread('DOM01-1.CSV');
data=strrep(data,char([13 10]),';');
data=split(string(data),';');
writematrix(data,'test.txt','filetype','text','QuoteStrings',1)
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., ...

Categorías

Más información sobre Data Import and Export en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Jul. de 2022

Comentada:

el 11 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by