Reading and analysing files in subfolders - indexing

31 visualizaciones (últimos 30 días)
A Wol
A Wol el 18 de Mayo de 2022
Comentada: Bjorn Gustavsson el 22 de Mayo de 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

Respuesta aceptada

Bjorn Gustavsson
Bjorn Gustavsson el 18 de Mayo de 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 comentarios
A Wol
A Wol el 22 de Mayo de 2022
Thanks so much, Bjorn. Works now!
Cheers
Bjorn Gustavsson
Bjorn Gustavsson el 22 de Mayo de 2022
Good, happy that it helped.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by