I want the following code to loop through multiple folders instead of having to open each folder individually so that I can do batch file processing.
4 views (last 30 days)
Show older comments
Tyler Lawson
on 10 Nov 2022
Commented: Tyler Lawson
on 10 Nov 2022
classdef SEdataAPPv1_01_Brooktrout < matlab.ui.componentcontainer.ComponentContainer
% Properties that correspond to underlying components
properties (Access = private, Transient, NonCopyable)
GridLayout matlab.ui.container.GridLayout
ASEDATAREDUCTIONLabel matlab.ui.control.Label
Version101Label matlab.ui.control.Label
Image matlab.ui.control.Image
PlotSaveNameEditField matlab.ui.control.EditField
PlotSaveNameEditFieldLabel matlab.ui.control.Label
TestPointLocationEditField matlab.ui.control.EditField
TestPointLocationEditFieldLabel matlab.ui.control.Label
StartButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: TestPointLocationEditField
function TestPointLocationEditFieldValueChanged(comp, event)
valueTP = comp.TestPointLocationEditField.Value;
end
% Value changed function: PlotSaveNameEditField
function PlotSaveNameEditFieldValueChanged(comp, event)
valuePS = comp.PlotSaveNameEditField.Value;
end
% Button pushed function: StartButton
function StartButtonPushed(comp, event)
% This code takes RAW SE data, turns the files into text (.txt) files and
% then combines the HIGH, MID, and LOW frequency into one text file which
% can be then multi-selected and those selected files will be put onto a
% plot.
% Enter the directory to search
directory = uigetdir('*',"Select Folder With Files To Be Processed");
% List all items in the folder
fileList = dir(directory);
% Delete the subfolders from the list (i.e. only keep files)
fileList(vertcat(fileList.isdir)) = [];
figure
hold all
% Uses folder as title of plot
[ParentFolderPath] = fullfile(directory);
[~, ParentFolderName] = fileparts(ParentFolderPath);
title(ParentFolderName,'FontSize',20);
% Loop through each file, copy it and give new extension: .txt
for i = 1:numel(fileList)
file = fullfile(directory, fileList(i).name);
[tempDir, tempFile] = fileparts(file);
status = copyfile(file, fullfile(tempDir, [tempFile, '.txt']));
end
% Combine HORIZONTAL text files
%location = input("location of test point: ","s");
fileName = comp.TestPointLocationEditField.Value + " HORZ NOM.txt";
dL = dir(fullfile(directory,'*HLF*NOM.txt')); % salt to suit wildcard to match naming convention
dM = dir(fullfile(directory,'*HMF*NOM.txt'));
dH = dir(fullfile(directory,'*HHF*NOM.txt'));
for i = 1:numel(inf) % there must be same number Lo, Mid, High -- add error check first
tD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
tD = [tD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
tD = [tD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% do whatever with each set here before going on...
writetable(tD,fullfile(directory, fileName));
plot(tD,"Frequency","SE","Color",'Blue',LineWidth=1.5);
end
% Combine VERTICAL text files
%location = input("location of test point: ","s");
fileName = comp.TestPointLocationEditField.Value + " VERT NOM.txt";
dL = dir(fullfile(directory,'*VLF*NOM.txt')); % salt to suit wildcard to match naming convention
dM = dir(fullfile(directory,'*VMF*NOM.txt'));
dH = dir(fullfile(directory,'*VHF*NOM.txt'));
for i = 1:numel(inf) % there must be same number Lo, Mid, High -- add error check first
tD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
tD = [tD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
tD = [tD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% do whatever with each set here before going on...
writetable(tD,fullfile(directory, fileName))
plot(tD,"Frequency","SE","Color",'Red',LineWidth=1.5);
end
% Combine AON text files and save as MR
%location = input("location of test point: ","s");
fileName = comp.TestPointLocationEditField.Value + " AON.txt";
dL = dir(fullfile(directory,'*HLF*AON.txt')); % salt to suit wildcard to match naming convention
dM = dir(fullfile(directory,'*HMF*AON.txt'));
dH = dir(fullfile(directory,'*HHF*AON.txt'));
for i = 1:numel(inf) % there must be same number Lo, Mid, High -- add error check first
tD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
tD = [tD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
tD = [tD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% do whatever with each set here before going on...
writetable(tD,fullfile(directory, fileName))
plot(tD,"Frequency","SE","Color",'#588146',LineWidth=1.5);
end
% Combine FTN text files and save as DR
%root = uigetdir();
%location = input("location of test point: ","s");
fileName = comp.TestPointLocationEditField.Value + " FTN.txt";
dL = dir(fullfile(directory,'*HLF*FTN.txt')); % salt to suit wildcard to match naming convention
dM = dir(fullfile(directory,'*HMF*FTN.txt'));
dH = dir(fullfile(directory,'*HHF*FTN.txt'));
for i = 1:numel(inf) % there must be same number Lo, Mid, High -- add error check first
tD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
tD = [tD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
tD = [tD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% do whatever with each set here before going on...
writetable(tD,fullfile(directory, fileName))
plot(tD,"Frequency","SE","Color",'#9f9f9f',LineWidth=1.5);
end
grid on
% Changing colors based on alphebetic order
newcolors = {'Blue','Red','#72a75b','#999999','#0003ff'};
colororder(newcolors);
% Sets pass/fail line
PFx = [10000 10000000 1000000000];
PFy = [20 80 80];
% Adds pass/fail line to plot
plot(PFx,PFy,'Color','Green','LineWidth',1.5);
% Changes legend location and removes pass/fail line from legend
Lgnd = legend('Location','northwest');
Lgnd = legend('Horizontal','Vertical','MR','DR','MIL-STD-188-125-1');
Lgnd.FontSize = 14;
% Labels both x and y axis
xlabel('Frequency (Hz)','FontSize',18,'FontWeight','bold');
ylabel('Shielding Effectivness (dB)','FontSize',18,'FontWeight','bold');
% Sets both axis scales
set(gca,'XScale','log','ylim',[-10 200]);
% Removes the values on the plot *Comment out
% set(gca,'xticklabel',[],'yticklabel',[]); to add them back in*
% set(gca,'xticklabel',[],'yticklabel',[]);
% Sets grid lines to dashes, colors
set(gca,'gridlinestyle','--');
set(gca,'gridalpha',0.5);
set(gca,'GridColor','Black');
set(gca,'Color','#ececec');
set(gca,'Color','#ececec');
hold off
0 Comments
Accepted Answer
Walter Roberson
on 10 Nov 2022
directory = uigetdir('*',"Select Folder With Files To Be Processed");
You could change that to
parentdirectory = uigetdir('*', 'Select Folder with folders inside it');
if isnumeric(parentdirectory); return; end %user cancel
dinfo = dir(parentdirectory);
dinfo(~[dinfo.isdir]) = []; %remove ordinary files
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and ..
for K = 1 : length(dinfo)
directory = fullfile(dinfo(K).folder, dinfo(K).name);
%now all of the normal steps
end
4 Comments
Walter Roberson
on 10 Nov 2022
Is the figure call inside the for K loop ? If so then are you getting one figure for every folder, or are you getting only one figure total? If you are getting one figure for every folder, then is the plot content the same for all of them?
I wonder if you did not fill in the place where I wrote
%now all of the normal steps
Inside that for K loop you should have all of the lines from
% List all items in the folder
through to the hold off
all inside the for K loop.
More Answers (0)
See Also
Categories
Find more on Environment and Settings 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!