MATLAB Answers


Opening files with randomly varying file names

Asked by Jacki
on 22 Feb 2011


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.

2 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

on 22 Feb 2011

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

Oleg Komarov
on 17 Jul 2011

Thanks, +1

Discover MakerZone

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

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!