MATLAB Answers


Opening files with randomly varying file names

Asked by Jacki
on 22 Feb 2011
Latest activity Answered by BHARGOB DEKA
on 11 Nov 2016 at 0:46


I am using Matlab to read in csv files generated during data collection. The files all have the format:


where the * is a randomly changing number from 0 to 9. There is no pattern to the number and I have A LOT of data files to open so I need to loop through them. Typically I would declare a filename and use strcat() with variables for any part of the filename that changed, but since I have no idea what the numbers will be I can't do this. The last number is non essential there is only one file per day.

Is there anyway to read in the files without knowing the last number?

i.e. filename = strcat('PR-2230A_',YYYY,'-',MM,'-',DD,'_00-08-',~,'.csv')

I would be greatful for any help!




No products are associated with this question.

3 Answers

Answer by Oleg Komarov
on 22 Feb 2011
 Accepted answer

I propose a different approach:

EDIT: forgot about the wildcard

% Retrieve all the files in a directory
names = dir('C:\Users\Oleg\Desktop\Nuova cartella\PR-2230A_YYYY-MM-DD_00-08-*.csv');
names = {};

Now names will contain only the files which begin with root and you can loop through all of them and load one by one.


  1 Comment

Worked GREAT thanks!!!!!!!!

Answer by Jim Hokanson
on 17 Jul 2011

Just in anyone else comes across this, use a wildcard instead:

d = dir('PR-2230A_YYYY-MM-DD_00-08-*.csv')
names = {};

The trick is that the dir() function supports wildcards.

  1 Comment

on 11 Nov 2016 at 0:46

So, How do I loop it over several file names?


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!