How to read table with specific word in name

4 views (last 30 days)
Hi there,
I have a folder full of .dat files. Many of these .dat files have the word "fluxall" in them, for example "PJG_2019_fluxall.dat". I would like to use the "readtable" function to read all the .dat files with the word "fluxall" in them. So far I have:
files = struct2cell(dir('*fluxall*.dat'));
filenames = char(files(1,:));
ind = cellstr(1:length(filenames));
for i = 1:length(filenames)
eval(['Table_',ind{i},' = readtable(',filenames{i},');']);
end
The idea is to create a table named "Table_1,2,3..." for each "fluxall".dat file. Obviously it doesnt work.
Thanks!
  3 Comments
Devon Fisher-Chavez
Devon Fisher-Chavez on 22 Nov 2019
My apologies.
The .dat files are comma separated timetables. My code is in a folder with a bunch of .dat files, some of them do not have the word "fluxall" in their title. I want my code to open all the .dat files with "fluxall" in the name. I have attached a .csv sample file... this website is not allowing me to attach a .dat.
Right now I am using using the following code to read them:
Js_PJ_TT_2017 = table2timetable(readtable('PJ_2017_fluxall.dat'));
Js_PJ_TT_2018 = table2timetable(readtable('PJ_2018_fluxall.dat'));
Js_PJ_TT_2019 = table2timetable(readtable('PJ_2019_fluxall.dat'));
However, I have a lot of these files and it would be nice to not have to enter all of them manually like this.
Stephen23
Stephen23 on 22 Nov 2019
Note that the code is rather fragile, e.g.
  1. It assumes that the structure returned by dir always contains the fields in the same order, but their order is not specified.
  2. Use of numbered variables is a sign that you are doing something wrong. Most likely you should just use indexing, which makes looping over data much simpler and more efficient (and is what the MATLAB documentation recommends instead of eval).
Note that use of eval to generate numbered variables is one way the beginners force themselves into writing slow, complex, buggy code that is hard to debug:

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 22 Nov 2019
Edited: Stephen23 on 22 Nov 2019
Much simpler and more robust than your code:
D = 'path to the directory where the files are saved';
S = dir(fullfile(D,'*fluxall*.dat'));
for k = 1:numel(S)
S(k).data = readtable(fullfile(D,S(k).name));
end
C = {S.data}; % optional
Note that keeping the data in the structure S has the significant advantage of keeping the data and filenames in correspondence.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!