How to reduce the line of the codes in the code given!
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi,
I am plotting 3 sets of data on each graph and plotting 4 different graphs by reading the columns from the csv files. but seems like my code is way too long. I am stuck. Can any one have a go and reduce the amount of lines in my code:
CODE:
files = { 'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_DesignPoint\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vr3i20t.000010.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_KickBack\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vs3ia8s.000010.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_flushflow\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vs5mz18.000010.csv'};
for i = 1:numel(files)
a = readtable(files{i});
X = a{:,2};
Y = a{:,13};
figure(1)
plot(X,Y,'*');
hold on
%Spacing and griding of the plot
xlim([0 350])
ylim([0 2])
hold on
grid on
end
for i = 1:numel(files)
a = readtable(files{i});
X = a{:,16};
Y = a{:,27};
figure(2)
plot(X,Y,'*');
hold on
%Spacing and griding of the plot
xlim([0 350])
ylim([0 2])
hold on
grid on
end
for i = 1:numel(files)
a = readtable(files{i});
X = a{:,30};
Y = a{:,41};
figure(3)
plot(X,Y,'*');
hold on
%Spacing and griding of the plot
xlim([0 350])
ylim([0 2])
hold on
grid on
end
for i = 1:numel(files)
a = readtable(files{i});
X = a{:,44};
Y = a{:,55};
figure(4)
plot(X,Y,'*');
hold on
%Spacing and griding of the plot
xlim([0 350])
ylim([0 2])
hold on
grid on
end
댓글 수: 0
채택된 답변
Voss
2022년 2월 6일
files = { ...
'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_DesignPoint\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vr3i20t.000010.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_KickBack\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vs3ia8s.000010.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_flushflow\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vs5mz18.000010.csv'; ...
};
col = [2 13; 16 27; 30 41; 44 55];
for j = 1:size(col,1)
for i = 1:numel(files)
a = readtable(files{i});
X = a{:,col(j,1)};
Y = a{:,col(j,2)};
figure(j)
plot(X,Y,'*');
hold on
end
%Spacing and griding of the plot
xlim([0 350])
ylim([0 2])
grid on
end
댓글 수: 1
Voss
2022년 2월 6일
Or, better, since you only need to readtable() each file once (a few more lines though):
files = { ...
'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_DesignPoint\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vr3i20t.000010.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_KickBack\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vs3ia8s.000010.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\ABCD_flushflow\Profile_Plot\Sheet_D\LSD_DataoverDistance.6vs5mz18.000010.csv'; ...
};
col = [2 13; 16 27; 30 41; 44 55];
for i = 1:numel(files)
a = readtable(files{i});
for j = 1:size(col,1)
X = a{:,col(j,1)};
Y = a{:,col(j,2)};
figure(j)
plot(X,Y,'*');
hold on
end
end
for j = 1:size(col,1)
%Spacing and griding of the plot
figure(j)
xlim([0 350])
ylim([0 2])
grid on
end
추가 답변 (1개)
William Rose
2022년 2월 6일
You read each of the three files four separate times. Read them in once at the beginning, to tables a1, a2, a3. Keep them in memory. Assuming that you have done that, then you could replace the first for loop with the following (delete the for loop entirely):
figure;
plot(a1{:,2},a1{:,13},'r*',a2{:,2},a2{:,13},'go',a3{:,2},a3{:,13},'b^');
xlim([0 350]), ylim([0 2]), grid on
legend('a1','a2','a3');
Do likewise for the remaining three for loops.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!