Not recognizing any text files in folder using dir(fullfile)
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to plot data from multiple text csv files in a folder. This is the first part of my code:
folder_path = 'C:users\power\downloads\star capillary files\'; % Replace with your folder path
% List all TXT files in the folder
txt_files = dir(fullfile(folder_path, '*.txt'));
for file_idx = 1:length(txt_files)
filename = fullfile(folder_path, txt_files(file_idx).name);
% Load the TXT file
data = readtable(filename, 'Delimiter', ',');
MATLAB is not recognizing that there are text files in my folder, though there are. I wrote a similar code to make calculations from a single txt file in that folder and it works. Does anyone know why it might not be recognizing txt files with this code? I added the folder to path in the MATLAB software by right clicking. Any help would be extremely appreciated!
1 Kommentar
Stephen23
am 9 Jul. 2024
"Does anyone know why it might not be recognizing txt files with this code?"
Compare:
'C:users\power\downloads\star capillary files\' % your path
'C:\users\power\downloads\star capillary files\' % valid Windows path
Antworten (2)
Voss
am 9 Jul. 2024
Try
folder_path = 'C:\users\power\downloads\star capillary files\';
instead of
folder_path = 'C:users\power\downloads\star capillary files\';
0 Kommentare
Image Analyst
am 9 Jul. 2024
Bearbeitet: Image Analyst
am 9 Jul. 2024
You're missing the forward slash after the drive letter and colon. Put that in. Also, your code is not very robust. You could increase the robustness by using isfolder, like
folder_path = 'C:\users\power\downloads\star capillary files\';
if ~isfolder(folder_path)
% Folder does not exist. Alert user:
errorMessage = sprintf('Error: this folder does not exist:\n"%s"\nPlease select an existing folder in the next window.', folder_path)
uiwait(errordlg(errorMessage));
% Ask user to pick a valid folder.
folder_path = uigetdir();
if folder_path == 0
% user clicked Cancel button.
return;
end
% If we get to here, then folder_path is valid.
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!