read specific files in a directory

3 views (last 30 days)
jsantoyoc
jsantoyoc on 10 Jun 2014
Commented: José-Luis on 11 Jun 2014
hallo, Anyone could please help me? I have a directory A, which contains many other files extentions, and 51 files *.vtk, named in order, I need to read line 1700 in all of the *vtk files and make another file (excel ) with all the 51 lines i have read from those files in the same order. Thanks a lot for any ideas, help! JC
  2 Comments
José-Luis
José-Luis on 10 Jun 2014
Edited: José-Luis on 10 Jun 2014
What have you tried so far?
doc dir
doc strcmp
doc fread
doc fgetl
doc xlswrite
jsantoyoc
jsantoyoc on 10 Jun 2014
Edited: the cyclist on 10 Jun 2014
hi, I am quite new with matlab but Ive just wrote some very specific lines, like these in order to open the files, however they do not work, thanks for your reply,
fid = fopen('dcinv.result_627','rt');
nlines = 0;
while (fgets(fid) ~= -1),
nlines = nlines+1;
end
fclose(fid);
C = textread('dcinv.result_627', '%s','delimiter', '\n');

Sign in to comment.

Answers (2)

dpb
dpb on 10 Jun 2014
If there is only one specific line of data you need and it is consistently a fixed offset into the file, then use the 'headerlines' parameter to skip the preceding lines. Since you know the number of files (either counted previously or by the length of the resulting dir structure returned), preallocate the array to hold the results and populate it as you process the files...
d=dir(fullfile('A','*.vtk');
N=length(d);
dat=zeros(N,numElementsInTargetRecord);
fmt=repmat('%f',1,numElementsInTargetRecord);
for i=1:N
dat(i,:)=readtext(fullfile('A',d(i).name),fmt,1,'headerlines',1699);
end
Or, if all you need do is to create the other file, you can dispense with saving the array in memory and just write the data to another file in format desired.
I created a format string above assuming numeric values only; salt to suit.
  2 Comments
jsantoyoc
jsantoyoc on 11 Jun 2014
Edited: Image Analyst on 11 Jun 2014
Thanks a lot Mr. dpb for your reply, however I still have not reached the objective of my code though. I have madeout something out of the two answers I have received so far. I would be so glad if you could please give me your opinion. Thanks a lot, JC
%Finding all .vtk files in directory
d=dir(fullfile('result140527_0932','*.vtk'));
N=length(d);
linePosition = 1;
cnt = 1;
for ii = d %Looping through files
fid = fopen(ii);
for ii= (1:1177); %Getting to the line you want
tline = fgetl(fid);
end
your_lines(cnt) = {tline}; %storing in cell array
fclose(fid)
cnt = cnt + 1;
end
José-Luis
José-Luis on 11 Jun 2014
This is more compact than mine. +1

Sign in to comment.


José-Luis
José-Luis on 10 Jun 2014
Edited: José-Luis on 10 Jun 2014
Ok, I'll get you started:
%Finding all .vtk files in directory
your_vtk_files = dir;
your_vtk_files = {your_vtk_files(:).name};
[ext ext ext] = cellfun(@(x) fileparts(x),your_vtk_files,'uniformoutput',false)
%Finding out .vtk
idx = strcmp('.vtk',ext);
your_vtk_files = your_vtk_files(idx);
your_lines = cell(sum(idx),1); %Pre-allocating
linePosition = 1;
cnt = 1;
for ii = your_vtk_files %Looping through files
fid = fopen(ii{:});
for ii = 1:linePosition %Getting to the line you want
tline = fgetl(fid);
end
your_lines(cnt) = {tline}; %storing in cell array
fclose(fid)
cnt = cnt + 1;
end
To write to excel, please look the documentation for xlswrite
  2 Comments
jsantoyoc
jsantoyoc on 11 Jun 2014
Edited: Image Analyst on 11 Jun 2014
Thanks a lot Mr. dpb for your reply, however I still have not reached the objective of my code though. I have madeout something out of the two answers I have received so far. I would be so glad if you could please give me your opinion. Thanks a lot, JC
%Finding all .vtk files in directory
d=dir(fullfile('result140527_0932','*.vtk'));
N=length(d);
linePosition = 1;
cnt = 1;
for ii = d %Looping through files
fid = fopen(ii);
for ii= (1:1177); %Getting to the line you want
tline = fgetl(fid);
end
your_lines(cnt) = {tline}; %storing in cell array
fclose(fid)
cnt = cnt + 1;
end
dpb
dpb on 11 Jun 2014
>> fullfile('result140527_0932','*.vtk')
ans =
result140527_0932\*.vtk
>>
fullfile concatenates a directory and a filename supplying the '\' automagically. As the above shows, it doesn't append the extension. I've often thought/wished there were a Matlab function that did, but there isn't. The example I gave used fullfile because your posting said your files were in a subdirectory 'A'. W/O that you just want
fnwild='result140527_0932*.vtk';
d=dir(fnwild);
where I put the name into a variable to make it simpler to modify/change.
for ii = d %Looping through files
fid = fopen(ii);
The above is simply invalid Matlab syntax...either use the construct I showed of iterating over length(d) and return d(i).name as the simplest or if you're enamored with the comma-separated list that Matlab will return as a structure element, the loop would have to be written as something like:
for s=[{d.name}] % a comma-separated list of cellstrings
fid = fopen(char(s)); % must convert cell string to char for *fopen*
...
I think the first is more straightforward and easier to remember--I'm always forgetting a set of [] or {} or the like in the other construct even so though it really looks cute, I think it's far simpler to just go "deadahead" and write it correctly the first time instead of having to spend time debugging the fancier solution to the same end result.
for ii= (1:1177); %Getting to the line you want
tline = fgetl(fid);
While this and fopen and friends works, it's much more overhead-intensive to use the line-by-line processing; I still recommend using the higher-level routines that have the facility built in via the named parameter 'headerlines'. That's another stylistic choice but one that I think can be shown to be a better performer for larger files. Take your pick...
I think the code I wrote in the first response should work as written; did you simply try it and if so, and there's something I overlooked, what was the error/unexpected result?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!