how can I open multiple text file only specific low and then save??
조회 수: 1 (최근 30일)
이전 댓글 표시
I would like to open 18 txt. files at once only numbers except first three characters 1-3 row.
Also, I want to save these files row 1,501~16,500 only. How can I do this? I would appreciate if somebody help me.
댓글 수: 2
Jan
2018년 3월 12일
I do not understand the question: "open 18 txt. files at once only numbers except first three characters 1-3 row" What does this mean?
채택된 답변
Akira Agata
2018년 3월 12일
Like this? Running the following code at the directory where your data (*.txt) is stored, it extracts 1501~16500 rows and saves as a new data file (*b.txt).
fileList = dir('*.txt');
for kk = 1:numel(fileList)
A = importdata(fileList(kk).name,'\t',3);
data = A.data(1501:16500,:);
[~,name,ext] = fileparts(fileList(kk).name);
fileName = [name,'b',ext];
dlmwrite(fileName,data,'precision',6);
end
댓글 수: 3
Akira Agata
2018년 3월 12일
Hi Houn-san,
Thank you for your response. The fileparts function returns file path, file name and file extension based on the input string. In this case, the input string fileList(kk).name contains only a file name and extension (e.g '01.txt'). So I have set the first output argument (=file path) as '~'. More details on fileparts function can be found here , but I would recommend try and see what happens by yourself, like:
>> [path,name,ext] = fileparts('01.txt')
path =
0×0 empty char array
name =
'01'
ext =
'.txt'
and
>> [path,name,ext] = fileparts('C:\folder\01.txt')
path =
'C:\folder'
name =
'01'
ext =
'.txt'
And, in this case, [name,'b',ext] returns '01b.txt'.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!