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

1 view (last 30 days)
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

Les Beckham
Les Beckham on 5 Jan 2022
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
Image Analyst
Image Analyst on 5 Jan 2022
In short, having ** allows it to recurse into subfolders rather than just searching one folder.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by