Reading in any file containing certain words in the file name

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

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.
Hi Blue thanks for responding! Can this still work with readtable? Readtable is something that is needed throughout the rest of the script. I’m wondering if I can have the Start1-3 variables to be equal to the files that contain their respective start1-3.csv given a different first letter, but still calling out readtable?

Sign in to comment.

Answers (1)

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

Products

Asked:

on 6 May 2020

Answered:

on 6 May 2020

Community Treasure Hunt

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

Start Hunting!