Processing txt multiple files in a folder and saving them using the original name but different format

조회 수: 3 (최근 30일)
Hi guys,
I'd like to process all files in a folder and do some sorting and save it in a subfolder with the original name but differnet file format.
I tried using dlmwrite to save at the end of my code. But it overwrites each loop.
The txt file is a strain data with a size 1326 X 304
Here is the structure of files
files =
56×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
Here is my code
% Convert raw data files into clean data with header
clc;clear; close('all')
files = dir(fullfile('*.txt'));
Pars.date = 1;
%% Loop
for i = 1:numel(files)
fid = fopen(files(i).name);
txt = textscan(fid,repmat('%f ',1,304),'delimiter','\n','HeaderLines',9);
rawdata = cell2mat(txt);
data = rawdata;
data(:,1:3) = round(data(:,1:3),6);
data =sortrows(data,1);
data = flipud(data);
X = data(:,4:end);
filename = files(i).name;
addpath('sorted_csv')
dlmwrite(filename.csv,X,'precision',16);
end
I'm not sure if I'm missing anything, Can anybody help? Let me know if you need additianal information to solve this
I also attached a sample txt file
  댓글 수: 2
Rik
Rik 2020년 10월 22일
Why are you using addpath? And why doesn't filename.csv result in an error?
Amanuel Hailu Mamo
Amanuel Hailu Mamo 2020년 10월 22일
It says
Dot indexing is not supported for variables of this type.
Error in rawdata_sort (line 38)
dlmwrite(filename.csv,X,'precision',16);

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

채택된 답변

Rik
Rik 2020년 10월 22일
Something like this should work:
%replace this:
filename = files(i).name;
addpath('sorted_csv')
dlmwrite(filename.csv,X,'precision',16);
%with this:
[~,name,ext]=fileparts(files(i).name);
outfilename=fullfile(files(i).folder,[name '.csv']);
dlmwrite(outfilename,X,'precision',16);
  댓글 수: 2
Amanuel Hailu Mamo
Amanuel Hailu Mamo 2020년 10월 22일
Thanks, what If I want to save it to a subfolder
Where do I add that function or ammend a function?
Does dlmwrite support that?
Rik
Rik 2020년 10월 22일
Did you have a look at the documentation for dlmwrite? Did you have a look at the content of the outfilename variable?
dlmwrite allows you to specify the full path, so if you change the path contained in outfilename you can specify a subfolder.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by