How to explain the code below in the loop so I reduce the lines

Hi,
Below code is reading the data from the different folders and csv files and plotting them onto one figure but I want to reduce the code and write into the loop so that loop will follow the path of each folder one by one and plot the data of the different excel files on one graph. you help will be appreciated.
Code:
close all; clear all; clc;
%plot the data for Data_LaserSheet_D
a = readtable('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\Data_Vector\Run 13-56-31.Adaptive PIV.6tvk0mbp\Export.6tvk0mbp.000000.csv');
X=a{:,5}/1000;
Y=a{:,6}/1000;
U=a{:,9};
V=a{:,10};
quiver(X,Y,U,V,10);
axis tight
hold on
%plot the data for Data_LaserSheet_C
a1 = readtable('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_C\Data_PIV\Data_Vector\Run 15-51-40.Adaptive PIV.6ty83hed\Export.6ty83hed.000000.csv');
X1=a1{:,5}/1000;
Y1=a1{:,6}/1000;
U1=a1{:,9};
V1=a1{:,10};
quiver(X1,Y1,U1,V1,10);
axis tight
hold on
%plot the data for Data_LaserSheet_B
a2 = readtable('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_B\Data_PIV\Data_Vector\Run 17-19-03.Adaptive PIV.6ul4ynyv\Export.6ul4ynyv.000000.csv');
X2=a2{:,5}/1000;
Y2=a2{:,6}/1000;
U2=a2{:,9};
V2=a2{:,10};
quiver(X2,Y2,U2,V2,10);
axis tight
hold on
%plot the data for Data_LaserSheet_A
a3 = readtable('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_A\Data_PIV\Data_Vector\Run 12-45-51.Adaptive PIV.6tskdx6a\Export.6tskdx6a.000000.csv');
X3=a3{:,5}/1000;
Y3=a3{:,6}/1000;
U3=a3{:,9};
V3=a3{:,10};
quiver(X3,Y3,U3,V3,10);
axis tight
hold on

 Accepted Answer

This would be a lot easier if all of the csv files were in the same folder. You could then use the dir cmd to get a list of the files and loop through them.
However, since you seem to know the exact files that you want to operate on, you can manually create this list first and then loop through the list to make the plot.
files = { 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\Data_Vector\Run 13-56-31.Adaptive PIV.6tvk0mbp\Export.6tvk0mbp.000000.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_B\Data_PIV\Data_Vector\Run 17-19-03.Adaptive PIV.6ul4ynyv\Export.6ul4ynyv.000000.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_B\Data_PIV\Data_Vector\Run 17-19-03.Adaptive PIV.6ul4ynyv\Export.6ul4ynyv.000000.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_A\Data_PIV\Data_Vector\Run 12-45-51.Adaptive PIV.6tskdx6a\Export.6tskdx6a.000000.csv'};
for i = 1:numel(files)
a = readtable(files{i});
X = a{:,5}/1000;
Y = a{:,6}/1000;
U = a{:,9};
V = a{:,10};
quiver(X, Y, U, V, 10);
axis tight
hold on
end

7 Comments

Thanks for your help. It really made life easier for me for the time being. Yes, it would have been easir if all the files would have been in one folder but the way data is exporting from the sensors is based on timestamp and each time stamp exported as folder containing excel files.
Is there more precise way to access the files as I will need to further analyse different csv files like Export.6tvk0mbp.000000.csv with different timestamps like Export.6tvk0mbp.000001.csv, Export.6tvk0mbp.000002.csv, Export.6tvk0mbp.000003.csv so on from each folder.
@Image Analyst. I have previously tried to work with the code in this forum but failed to complete it and gave up. Code from the forum shape up something like below where I gave a path of the folder in which all the 4 subfolders are containing csv files. whenever i run that code said there is no csv file in that folder which is true bc they are in subfolders. path for the subfolders are given below.
F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\Data_Vector\Run 13-56-31.Adaptive PIV.6tvk0mbp\Export.6tvk0mbp.000000.csv'; ...
F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_B\Data_PIV\Data_Vector\Run 17-19-03.Adaptive PIV.6ul4ynyv\Export.6ul4ynyv.000000.csv'; ...
F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_B\Data_PIV\Data_Vector\Run 17-19-03.Adaptive PIV.6ul4ynyv\Export.6ul4ynyv.000000.csv'; ...
F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_A\Data_PIV\Data_Vector\Run 12-45-51.Adaptive PIV.6tskdx6a\Export.6tskdx6a.000000.csv';
Code tried:
myFolder = 'F:\3-PIV_Experimental_Data\Outlet_110';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
a = readtable(fullFileName{i});
X = a{:,5}/1000;
Y = a{:,6}/1000;
U = a{:,9};
V = a{:,10};
quiver(X, Y, U, V, 10);
axis tight
hold on
end
"You could then use the dir cmd to get a list of the files and loop through them."
Using DIR does not require the files to be in the same directory:
filePattern = fullfile(myFolder, '**', '*.csv');
% ^^^^
what does '**' means in this case?
I have tried it but getting error further can you help @Stephen
Code:
myFolder = 'F:\3-PIV_Experimental_Data\Outlet_110';
filePattern = fullfile(myFolder, '**', '*.csv');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
a = readtable(fullFileName{k});
X = a{:,5}/1000;
Y = a{:,6}/1000;
U = a{:,9};
V = a{:,10};
quiver(X, Y, U, V, 10);
axis tight
hold on
end
Error:
Brace indexing is not supported for variables of this type.
Error in read_csv (line 38)
a = readtable(fullFileName{k});
"what does '**' means in this case? "
Searches directories recursively, just as the documentation states:
"Brace indexing is not supported for variables of this type."
FullFileName is the chararacter vector returned by FULLFILE, why are you trying to use curly braces on a character vector? Anyway, you have already used indexing into theFiles, so why repeat the indexing again?
a = readtable(fullFileName{k});
% ^^^ get rid of these
In short, having ** allows it to recurse into subfolders rather than just searching one folder.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!