Reading in any file containing certain words in the file name
Show older comments
I currently have a script that reads in specific .csv files from the directory the script runs in. The .csv files are very similar except for the first letter that changes. In this example, the script is hardcoded to csv files that begin with 'a' within it's directory, but i'm looking to make this script work with any first letter.
start1 = readtable('a-start1.csv')
start2 = readtable('a-start2.csv')
start3 = readtable('a-start3.csv')
Is it possible to use readtable and use * like readtable('*-start1.csv) of some sort?
Thank you so much for the help, matlab is very new to me!
2 Comments
Blue
on 6 May 2020
I think I would go with :
all_files = dir(fullfile('*-start*.csv'));
files_names = {all_files.name};
and then you can loop over all the files that were found. But maybe I misunderstood your question.
ArtLabbe12
on 6 May 2020
Answers (1)
Ameer Hamza
on 6 May 2020
If you still want to maintain start1, start2 and start3 in the rest of your script, then you can do something like this
files_1 = dir('*-start1.csv');
files_2 = dir('*-start1.csv');
files_3 = dir('*-start3.csv');
files_1 = {files_1.name};
files_2 = {files_2.name};
files_3 = {files_3.name};
for i=1:numel(numel(files_1)) % write a loop to iterate through all files
start1 = readtable(files_1{i})
start2 = readtable(files_2{i})
start3 = readtable(files_3{i})
% rest of your current code
end
Categories
Find more on Standard File Formats in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!