Import files in a folder containing certain extension

Hello, I have many csv files. I am looking to write a couple lines to import specific ones.
There are 0-39 files for each time step (1-5). For example, the format 'pp0.1.csv' indicates file 0/39 for time step 1. I want to import all of the files containing the end '.5.csv' into matlab. Thus pp0-pp39, with that extension. They are all in the same folder. Thanks.

Answers (1)

P = 'absolute/relative path to where the files are saved';
S = dir(fullfile(P,'*.5.csv'));
S = natsortfiles(S); % optional, see below.
for k = 1:numel(S)
F = fullfile(P,S(k).name);
S(k).data = csvread(F);
end
If you want the files loaded in numeric order then download my FEX submission natsortfiles:

7 Comments

I don't particularly care about the order they are imported. Is there something I should change directly about the first script? When I run it, it produces C (0x1 cell), k ( []), S (0x1 struct).
Where are the files located? You might need to add the directory path:
P = '...'; % the directory where the files are.
S = dir(fullfile(P,'*.5.csv'));
C = cell(size(S));
for k = 1:numel(S)
C{k} = csvread(fullfile(P,S(k).name));
end
is fullfile a command or something I need to change? Now C and S are 40x1, and P is a character with my path directory.
"Now C and S are 40x1"
Then you all of your data has been imported correctly. Each matrix is stored in one cell of the cell array C, and you can access it using cell array indexing, e.g.:
C{1} % 1st matrix
C{2} % 2nd matrix
...
or process them in a loop, as per my answer. If all of those matrices have the same size then you could also concatenate them into one 3D array (which may be useful for you), e.g.:
arr = cat(3,C{:}).
When I run the initial code, I still get a bunch of errors. I get
"Error using dlmread (line 147) Mismatch between file and format character vector. Trouble reading 'Numeric' field from file (row number 1, field number 1) ==> "vel:0","vel:1","vel:2","Gradients:0","Gradients:1","Gradients:2","Gradients:3","Gradients:4","Gradients:5","Gradients:6","Gradients:7","Gradients:8","Result","Points:0","Points:1","Points:2"\n
Error in csvread (line 48) m=dlmread(filename, ',', r, c);
Error in untitled (line 5) C{k} = csvread(fullfile(P,S(k).name)); "
That list of vel0, gradients, etc is the first row of the csv. It's the titles to each of the columns. I have no idea what this means.
If it would make things simpler- I actually only want the data from the 13, 15 and 16th columns from all 40 of the files. That's the "Result" , " Points 1" and "Points 2" columns respectively.
"When I run the initial code, I still get a bunch of errors."
This is the first time that you have mentioned that you are getting an error message. It seems that the file you are trying to read cannot be read by csvread, probably because the file uses double quotes around numeric values (whereas csvread requires numeric values to be without quotes). You could probably use textscan or readtable instead. If you want help with this then please make a new comment, and upload a sample file by clicking on the paperclip button.

Sign in to comment.

Categories

Tags

Asked:

on 28 Feb 2018

Edited:

on 26 Apr 2021

Community Treasure Hunt

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

Start Hunting!