Change code to grab a single file

2 次查看(过去 30 天)
Tyler
Tyler 2022-12-21
回答: Rahul 2025-3-18
The current code takes 3 files and puts them into 1 (SEE COMBINE HORIIZONTAL SECTION) and then reads that newly created file and plots it. However, I need to change it so that it only grabs a single file without the combination process. The folder has the files I need to plot already and no combination of files is needed.
% 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)) = [];
app.Lamp.Color = 'Yellow'
figure
hold on
% Uses folder as title of plot
[ParentFolderPath] = fullfile(directory);
[~, ParentFolderName] = fileparts(ParentFolderPath);
% 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
if app.HorizontalButton.Value == 1;
fileName = ParentFolderName + " HORZ NOM.txt";
dL = dir(fullfile(directory,'*HLF*NOM.txt'));
dM = dir(fullfile(directory,'*HMF*NOM.txt'));
dH = dir(fullfile(directory,'*HHF*NOM.txt'));
for i = 1:numel(inf)
HORZtD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
HORZtD = [HORZtD;readtable(fullfile(dM(i).folder,dM(i).name),'numheaderlines',6,'readvariablenames',1)];
HORZtD = [HORZtD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
writetable(HORZtD,fullfile(directory, fileName));
% Adding the line to the plot with options for color and marker
if app.HorizontalMarkersButton.Value == 1;
Hplot = plot(HORZtD.Frequency,HORZtD.SE,"Color",app.HorizontalColorDropDown.Value,'LineWidth',str2num(app.LineWidthDropDown.Value), ...
Marker=app.HorizontalMarkerDropDown.Value,DisplayName='Horizontal');
else
Hplot = plot(HORZtD.Frequency,HORZtD.SE,"Color",app.HorizontalColorDropDown.Value,'LineWidth',str2num(app.LineWidthDropDown.Value), ...
DisplayName='Horizontal');
hold on
end
end
end

回答(1 个)

Rahul
Rahul 2025-3-18
Hi @Tyler,
As I understand the current code combines the files from the selected folder which is not desired according to the new workflow. As per the question, you require only to select a single file from the directory and process its data for plotting.
After reviewing your code, I observe that the user only manually selects the directory and the files get auto-selected based on their naming convention. If you require the user to select the file required manually as well, then 'uigetfile' will help in acheiving this.
Alternatively, if the file of only a particular naming comnevntion/extension is required to be selected, then the index of that file can be selected by using the 'filelist' variable as defined in the given code.
Node: The for loop going f5rom 1:numel(inf) would not be required as we are not combining multiple files in the new use-case.
The following MathWorks documentations can be referred to know more:
Thanks.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by