Asked by Noah Wilson
on 25 Jun 2019

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

Answer by Murugan C
on 26 Jun 2019

Accepted Answer

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

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## dpb (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/468889-help-forming-loop-to-simplify-my-code#comment_718304

## Stephen Cobeldick (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/468889-help-forming-loop-to-simplify-my-code#comment_718475

Sign in to comment.