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

1 次查看(过去 30 天)
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

采纳的回答

Les Beckham
Les Beckham 2022-1-5
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 个评论
Stephen23
Stephen23 2022-1-5
编辑:Stephen23 2022-1-5
"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
Image Analyst
Image Analyst 2022-1-5
In short, having ** allows it to recurse into subfolders rather than just searching one folder.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by