Help forming loop to simplify my code
1 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!