converting multiple textfiles to csv and xlsx format.
1 次查看(过去 30 天)
显示 更早的评论
I have the code for the one file. how can I loop it through?
Here is the code.
filename = 'myFile.txt';
format = '%c';
data=readlines(filename);
data = replace(data, ",", " ");
% data=strrep(data,char(9),',');
%mat = char(data1);
%int = str2double(data1);
writelines(data, 'myFile.csv');
opts = delimitedTextImportOptions("NumVariables", 522);
% Specify range and delimiter
otps.row = [3, 33];
otps.Delimiter = " ";
% Specify column names and types
opts.VNames = ["bin_depth", "depth", "wt", "salinity", "stimf"]
otps.VTypes = ["double", "double", "double", "double", "double"]
table = readtable( "myFile.csv", opts);
writetable(table, 'myFile.xlsx', 'sheet',1,'Range','A1')
I also want to retain the respective names of the text files.
2 个评论
Walter Roberson
2022-9-17
Why would you bother reading the file, changing the delimiter to space, writing the file, reading the file, writing the file?
Why not just readtable() specifying the VariableNames and writetable that? If you need to skip lines you can do that when reading from the original file.
You create options for 522 columns but you only set variable names for 4, which will be a problem.
采纳的回答
Walter Roberson
2022-9-17
编辑:Walter Roberson
2022-9-17
format long g
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1128290/f64d5aab60_IOP_20130909_2155.txt';
Lines = readlines(filename);
%variable names are on the first line
variablenames = regexp(Lines(1), ',', 'split');
variablenames(1:5) = ["bin_depth", "depth", "wt", "salinity", "stimf"];
%get rid of lines that do not start with a number
Lines( cellfun(@isempty, regexp(Lines, '^\d')) ) = [];
%we are going to use textscan(), which needs the text as a continuous
%string, not an array
as_text = strjoin(Lines, newline);
%textscan can figure out how many columns there are if you use empty format
%textscan returns cell array; we convert that to array and array to table
fmt = '';
data = array2table( cell2mat( textscan(as_text, fmt)), 'VariableNames', variablenames);
writetable(data, 'myFile.xlsx', 'sheet',1,'Range','A1')
3 个评论
Walter Roberson
2022-9-19
dinfo = dir('*IOP*.txt');
filenames = fullfile({dinfo.folder}, {dinfo.name});
num_files = length(filenames);
for K = 1 : num_files
filename = filenames{K};
Lines = readlines(filename);
and so on
end
But be careful -- you are not going to be overwriting the same column of the same sheet of the same file. Perhaps use 'sheet', K instead of 'sheet', 1
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!