Import files in a folder containing certain extension
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (1)
Stephen23
am 28 Feb. 2018
Bearbeitet: Stephen23
am 26 Apr. 2021
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 Kommentare
Stephen23
am 2 Mär. 2018
"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.
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!