No such file or directory for a found file.

3 views (last 30 days)
Stephen West
Stephen West on 20 Oct 2021
Answered: Benjamin on 20 Oct 2021
files = dir("Aero Lab Airfoil Testing/2002 Aero Lab 2 - Group Data/*.csv");
for n=1:length(files)
load(files(n).name, "-ascii");
end
I have this code to load in and read several .csv files at once. When I run it, I get the error
>> airfoil
Error using load
Unable to read file 'AirfoilPressure_S301_1.csv'. No such file or directory.
Error in airfoil (line 3)
load(files(n).name, "-ascii");
I'm not sure how there could be an issue with the file not existing when the code is locating the file in the directory. Any help would be appreciated.

Answers (2)

Image Analyst
Image Analyst on 20 Oct 2021
Try this:
topLevelFolder = fullfile(pwd, 'Aero Lab Airfoil Testing/2002 Aero Lab 2 - Group Data');
filePattern = fullfile(topLevelFolder, '**\*.csv');
fileList = dir(filePattern);
for k = 1 : numel(fileList)
thisFullFileName = fullfile(fileList(k).folder, fileList(k).name);
fprintf('Reading %s...\n', thisFullFileName);
data = readmatrix(thisFullFileName); % or csvread()
end

Benjamin
Benjamin on 20 Oct 2021
files(n).name is just the file name, not the full path to the file, so you have to cd to the directory and/or refer to the file by its full path

Community Treasure Hunt

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

Start Hunting!