Help forming loop to simplify my code
1 view (last 30 days)
Show older comments
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 Comments
dpb
on 26 Jun 2019
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
on 26 Jun 2019
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.
Accepted Answer
Murugan C
on 26 Jun 2019
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 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!