how can I open multiple text file only specific low and then save??
1 view (last 30 days)
Show older comments
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 Comments
Jan
on 12 Mar 2018
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?
Accepted Answer
Akira Agata
on 12 Mar 2018
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 Comments
Akira Agata
on 12 Mar 2018
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'.
More Answers (0)
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!