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

New to MATLAB?

Opening files with randomly varying file names

Asked by Jacki

Jacki

on 22 Feb 2011
Accepted Answer by Oleg Komarov

Oleg Komarov

Hello,

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

PR-2230A_YYYY-MM-DD_00-08-*.csv

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!

0 Comments

Jacki

Jacki

Tags

Products

No products are associated with this question.

2 Answers

Answer by Oleg Komarov

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 = {names.name};

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

Oleg

1 Comment

Jacki

Jacki

on 22 Feb 2011

Worked GREAT thanks!!!!!!!!

Oleg Komarov

Oleg Komarov

Answer by Jim Hokanson

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 = {d.name};

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

1 Comment

Oleg Komarov

Oleg Komarov

on 17 Jul 2011

Thanks, +1

Jim Hokanson

Jim Hokanson

Contact us