How to reduce the line of the codes in the code given!

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Voss
Voss il 6 Feb 2022
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 Commento
Voss
Voss il 6 Feb 2022
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

Accedi per commentare.

Più risposte (1)

William Rose
William Rose il 6 Feb 2022
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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by