Help forming loop to simplify my code

조회 수: 1 (최근 30일)
Noah Wilson
Noah Wilson 2019년 6월 25일
댓글: Stephen23 2019년 6월 26일
This is the code I currently have to pull and graph some data I have. As you can see it is the same code repeated 5 times for the five different files. I know it is a lot of code for a simple job and thus why I am seeking help. Is there some sort of loop I would be able to turn this into to reduce the amount of code and simplfy it? I am still learning Matlab so I apologize if the code is clunky. Thank you for the help in advanced!
%Workspace must be cleared before using this script
clear
clc
%Folders containing data of interest
Folder1 = '';
Folder2 = '';
Folder3 = '';
Folder4 = '';
Folder5 = '';
%These will be displayed in the legend of the graph
%Times should be in the same order as the corresponding folders above.
time1 = '2PM';
time2 = '8AM';
time3 = '9PM';
time4 = '11AM';
time5 = '11PM';
%=============================================================================================
%Changes to first folder and then to Finaldata folder
cd(Folder1)
cd('Finaldata')
%Searches for SizeChem data
SizeChemfile = dir('*SizeChem.dat');
SizeChemdata = SizeChemfile.name;
SizeChem = importdata(SizeChemdata);
%Calculates the length of SizeChem data
total = length(SizeChem).*0.1;
%Replaces first column with zeros
SizeChem(:,1) = 0 ;
newcolumn = sum(SizeChem,2);
for i = 1:length(SizeChem)
T(i) = sum(newcolumn(1:i,1));
end
T = T(:,1:length(SizeChem));
sums = (T')./T(end,end);
%Replaces zeros in first column with correct diameters
SizeChem(:,1) = 0.05:0.1:total ;
plot(SizeChem(:,1), sums)
hold on
%=============================================================================================
%Changes to first folder and then to Finaldata folder
cd(Folder2)
cd('Finaldata')
%Searches for SizeChem data
SizeChemfile = dir('*SizeChem.dat');
SizeChemdata = SizeChemfile.name;
SizeChem = importdata(SizeChemdata);
%Calculates the length of SizeChem data
total = length(SizeChem).*0.1;
%Replaces first column with zeros
SizeChem(:,1) = 0 ;
newcolumn = sum(SizeChem,2);
for i = 1:length(SizeChem)
T(i) = sum(newcolumn(1:i,1));
end
T = T(:,1:length(SizeChem));
sums = (T')./T(end,end);
%Replaces zeros in first column with correct diameters
SizeChem(:,1) = 0.05:0.1:total ;
plot(SizeChem(:,1), sums)
%=============================================================================================
%Changes to first folder and then to Finaldata folder
cd(Folder3)
cd('Finaldata')
%Searches for SizeChem data
SizeChemfile = dir('*SizeChem.dat');
SizeChemdata = SizeChemfile.name;
SizeChem = importdata(SizeChemdata);
%Calculates the length of SizeChem data
total = length(SizeChem).*0.1;
%Replaces first column with zeros
SizeChem(:,1) = 0 ;
newcolumn = sum(SizeChem,2);
for i = 1:length(SizeChem)
T(i) = sum(newcolumn(1:i,1));
end
T = T(:,1:length(SizeChem));
sums = (T')./T(end,end);
%Replaces zeros in first column with correct diameters
SizeChem(:,1) = 0.05:0.1:total ;
plot(SizeChem(:,1), sums)
%=============================================================================================
%Changes to first folder and then to Finaldata folder
cd(Folder4)
cd('Finaldata')
%Searches for SizeChem data
SizeChemfile = dir('*SizeChem.dat');
SizeChemdata = SizeChemfile.name;
SizeChem = importdata(SizeChemdata);
%Calculates the length of SizeChem data
total = length(SizeChem).*0.1;
%Replaces first column with zeros
SizeChem(:,1) = 0 ;
newcolumn = sum(SizeChem,2);
for i = 1:length(SizeChem)
T(i) = sum(newcolumn(1:i,1));
end
T = T(:,1:length(SizeChem));
sums = (T')./T(end,end);
%Replaces zeros in first column with correct diameters
SizeChem(:,1) = 0.05:0.1:total ;
plot(SizeChem(:,1), sums)
%=============================================================================================
%Changes to first folder and then to Finaldata folder
cd(Folder5)
cd('Finaldata')
%Searches for SizeChem data
SizeChemfile = dir('*SizeChem.dat');
SizeChemdata = SizeChemfile.name;
SizeChem = importdata(SizeChemdata);
%Calculates the length of SizeChem data
total = length(SizeChem).*0.1;
%Replaces first column with zeros
SizeChem(:,1) = 0 ;
newcolumn = sum(SizeChem,2);
for i = 1:length(SizeChem)
T(i) = sum(newcolumn(1:i,1));
end
T = T(:,1:length(SizeChem));
sums = (T')./T(end,end);
%Replaces zeros in first column with correct diameters
SizeChem(:,1) = 0.05:0.1:total ;
plot(SizeChem(:,1), sums)
%===========================================================================================
xlabel('Diameter (\mum)', 'FontSize', 18)
ylabel('Counts(%)', 'FontSize', 18)
title('Particle Size Distribution', 'FontSize', 20)
legend({time1, time2, time3, time4, time5}, 'Location', 'best', 'FontSize', 13)
hold off
  댓글 수: 2
dpb
dpb 2019년 6월 26일
Sure, just iterate through the returned list of files with a appropriate dir() wild card expression.
However, it looks to me as though your script will just process the same set(s) of files fie times--since the 'FolderN" variables are all empth, the two cd commnands are all going to end up in the same 'FinalData' subdirectory("folder") and the dir('*SizeChem.dat') call is then going to return the identical list of files each time.
Stephen23
Stephen23 2019년 6월 26일
Avoid cd in code. It slows everything down, can change what functions are called, and it makes debugging harder. All functions that import/export data to/from files accept absolute/relative filenames, you should use them.

댓글을 달려면 로그인하십시오.

채택된 답변

Murugan C
Murugan C 2019년 6월 26일
Hello Wilson, Please try below code.
clc
clear
%These will be displayed in the legend of the graph
%Times should be in the same order as the corresponding folders above.
time1 = '2PM';
time2 = '8AM';
time3 = '9PM';
time4 = '11AM';
time5 = '11PM';
%lists all files with a .dat extension zero or more directories under the current directory.
SizeChemfile = dir('**/*SizeChem.mat');
figure %open figure for plotting
for i = 1 : length(SizeChemfile) % getting length of .dat files.
SizeChemdata = SizeChemfile(i).name;
SizeChem = importdata(SizeChemdata);
%Calculates the length of SizeChem data
total = length(SizeChem).*0.1;
%Replaces first column with zeros
SizeChem(:,1) = 0 ;
newcolumn = sum(SizeChem,2);
for i = 1:length(SizeChem)
T(i) = sum(newcolumn(1:i,1));
end
T = T(:,1:length(SizeChem));
sums = (T')./T(end,end);
%Replaces zeros in first column with correct diameters
SizeChem(:,1) = 0.05:0.1:total ;
hold on
plot(SizeChem(:,1), sums)
%===========================================================================================
xlabel('Diameter (\mum)', 'FontSize', 18)
ylabel('Counts(%)', 'FontSize', 18)
title('Particle Size Distribution', 'FontSize', 20)
end
legend({time1, time2, time3, time4, time5}, 'Location', 'best', 'FontSize', 13)
hold off

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by