How to read and plot the data from csv files of subfolders?

25 views (last 30 days)
Hi,
I am trying to read the csv files from the 48 subfolders and plot the data from each folder csv file onto the graph but it is only plotting the data from one file. I am not sure where I am going wrong? can anyone help? Below is the code.
Code:
files = subdir('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\5-AverageFilter_VelocitiesExtration\Point_Velocities\Uy\*.csv');
for i = 1:numel(files)
filename = files(i).name;
data = readmatrix(filename);
Uy=data(1,2);
time = data(1,1);
plot(time,Uy,'*')
end

Accepted Answer

Walter Roberson
Walter Roberson on 9 Mar 2022
Edited: Walter Roberson on 9 Mar 2022
projectdir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\5-AverageFilter_VelocitiesExtration\Point_Velocities';
files = dir( fullfile(projectdir, '*', '*.csv') );
filenames = fullfile({files.folder}, {files.name});
num_files = length(filenames);
all_Uy = zeros(num_files,1);
all_time = zeros(num_files,1);
for i = 1:numel(files)
filename = filenames{i};
data = readmatrix(filename);
all_Uy(i) = data(1,2);
all_time(i) = data(1,1);
end
scatter(all_time, all_Uy, '*');
  2 Comments
Walter Roberson
Walter Roberson on 10 Mar 2022
If so then the equivalent would be
files = dir( fullfile(projectdir, '**', '*.csv') );
Anyhow, the error was that you were only pulling out the file name from the directory information, without pulling out information about which directory the file was in.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!