Reading and analysing files in subfolders - indexing
18 views (last 30 days)
Show older comments
A Wol
on 18 May 2022
Commented: Bjorn Gustavsson
on 22 May 2022
I'm trying to find and open *.tab files containing a string in their filenames, in several sub-folders. I have tried various options online, but always get errors. I think my issues is that I don't understand indexing in for loops. The code below gives the error "Error using fopen First input must be a file name or a file identifier." on line 6 (fname).
I've already tried:
Any help is greatly appreciated!
Closest code:
dt = 0.005;
tt = 0:dt:60;
files = dir(fullfile('...\**\*xacc.tab')); %finds 86 files with xacc string;
FileName = fullfile({files.folder},{files.name});%count = 0;
for k = 1:numel(files)
fname = FileName(k);
fid(k) = fopen(fname, 'r'); %I don't know how to open files based on name in files variable
data_xacc = textscan(fid(k),'%*f%f%f','delimiter',' ','headerlines',2); %I have tried other options such as dlmread, etc. but don't work given .tab extension.
fclose(fid(k));
time{k} = data_xacc{1,k}{1,1}; %I want to be able to extract column vectors from each tab file to analyse/interpolate.
xacc{k} = data_xacc{1,k}{1,2};
xacc{k} = interp1(time{k},xacc{k},tt,'linear','extrap');
end
0 Comments
Accepted Answer
Bjorn Gustavsson
on 18 May 2022
Why not skip the generation of FileName and index into files directly inside the loop:
dt = 0.005;
tt = 0:dt:60;
files = dir(fullfile('...\**\*xacc.tab')); %finds 86 files with xacc string;
FileName = fullfile({files.folder},{files.name});%count = 0;
for k = 1:numel(files)
fname = fullfile(files(k).folder,files(k).name); % generate filename for the k-th file
%fname = FileName(k);
fid = fopen(fname, 'r'); % Seems unnecessary to keep all fid-s
data_xacc = textscan(fid,'%*f%f%f','delimiter',' ','headerlines',2); %I have tried other options such as dlmread, etc. but don't work given .tab extension.
fclose(fid); % since you instantly close them after reading, they will not be useful later afaik
time{k} = data_xacc{1,k}{1,1}; %I want to be able to extract column vectors from each tab file to analyse/interpolate.
xacc{k} = data_xacc{1,k}{1,2};
xacc{k} = interp1(time{k},xacc{k},tt,'linear','extrap');
end
HTH
6 Comments
More Answers (0)
See Also
Categories
Find more on Logical 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!