How to read and plot the data from csv files of subfolders?
11 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2022-3-9
编辑:Walter Roberson
2022-3-9
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 个评论
Walter Roberson
2022-3-10
Were you using subdir() from the File Exchange? https://www.mathworks.com/matlabcentral/fileexchange/15859-subdir-a-recursive-file-search
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!