How to read table with specific word in name

5 Ansichten (letzte 30 Tage)
Devon Fisher-Chavez
Devon Fisher-Chavez am 22 Nov. 2019
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 Kommentare
Devon Fisher-Chavez
Devon Fisher-Chavez am 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 am 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:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 22 Nov. 2019
Bearbeitet: Stephen23 am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by