Have code check if file exists and if not, continue
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I know the syntax isfile and isfolder should allow me to move on if the file doesn't exsist. However I cannot find where to put it into my code.
Info on what the code is doing:
- It takes 2 files, one with HLF_BX and one with HHF_BX in the name and combines them. Then makes a new file for the combined data, then plots that data.
- This happens for HLF_BY and HHF_BY and also HLF_BZ and HHF_BZ.
Problem is that if one of those sets of files are missing (for example, no BX data, only BY and BZ), then the code throws an error and stops.
Where do I put the check for if the file exsists into the code?
if app.SingleFolderButton.Value == 1
% 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.HorizontalXButton.Value == 1
fileName = ParentFolderName + " BX HORZ NOM.txt";
dL = dir(fullfile(directory,'*HLF*BX NOM.txt'));
dH = dir(fullfile(directory,'*HHF*BX NOM.txt'));
for i = 1:numel(inf)
HORZtD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
HORZtD = [HORZtD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
% Saves the newly created combined file to use for the plot
writetable(HORZtD,fullfile(directory, fileName));
% Adding the line to the plot with various options
if app.IncludeonLegendCheckBoxHORZX.Value == 1
if app.HorizontalMarkersButton.Value == 1
Hplot = plot(HORZtD.Frequency,HORZtD.SE,"Color",app.HorizontalXColorDropDown.Value,'LineWidth',str2double(app.LineWidthDropDown.Value), ...
Marker=app.HorizontalMarkerDropDown.Value,DisplayName='BX');
else
Hplot = plot(HORZtD.Frequency,HORZtD.SE,"Color",app.HorizontalXColorDropDown.Value,'LineWidth',str2double(app.LineWidthDropDown.Value), ...
DisplayName='BX');
end
else
if app.HorizontalMarkersButton.Value == 1
Hplot = plot(HORZtD.Frequency,HORZtD.SE,'HandleVisibility','off',"Color",app.HorizontalXColorDropDown.Value,'LineWidth',str2double(app.LineWidthDropDown.Value), ...
Marker=app.HorizontalMarkerDropDown.Value);
else
Hplot = plot(HORZtD.Frequency,HORZtD.SE,'HandleVisibility','off',"Color",app.HorizontalXColorDropDown.Value,'LineWidth',str2double(app.LineWidthDropDown.Value));
end
hold on
end
end
end
0 Commenti
Risposte (1)
dpb
il 12 Gen 2023
Modificato: dpb
il 12 Gen 2023
...
dL = dir(fullfile(directory,'*HLF*BX NOM.txt'));
dH = dir(fullfile(directory,'*HHF*BX NOM.txt'));
for i = 1:numel(inf)
HORZtD = readtable(fullfile(dL(i).folder,dL(i).name),'numheaderlines',6,'readvariablenames',1);
HORZtD = [HORZtD;readtable(fullfile(dH(i).folder,dH(i).name),'numheaderlines',6,'readvariablenames',1)];
...
The variable inf is undefined in the code snippet given; it appears it must be based on some total number of files found earlier.
There would appear to be your logic problem; what you need to do is to ensure the number of elements in dL and dH is the same and then iterate over numel(dL) -- then you will be assured to only be operating on files that actually do exist.
Approaching it that way, you don't need to explicitly test for the file; the dir() search will have done that for you already; all you need to check is that both sets are there in the same number of each.
NB: In the following
dL = dir(fullfile(directory,'*HLF*BX NOM.txt'));
dH = dir(fullfile(directory,'*HHF*BX NOM.txt'));
you have coded the "X" direction explicitly and then duplicate for the other directions. You could simplify the amount of code significantly by putting those 'X', 'Y','Z' values in a lookup table and building the search match string dynamically in a loop instead of duplicating the same code three times.
0 Commenti
Vedere anche
Categorie
Scopri di più su File Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!