Not recognizing any text files in folder using dir(fullfile)

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 comentario

"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

Iniciar sesión para comentar.

Respuestas (2)

Voss
Voss el 9 de Jul. de 2024

Try

folder_path = 'C:\users\power\downloads\star capillary files\';

instead of

folder_path = 'C:users\power\downloads\star capillary files\';
Image Analyst
Image Analyst el 9 de Jul. de 2024
Editada: Image Analyst el 9 de Jul. de 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
Then do your call to dir.

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Preguntada:

el 9 de Jul. de 2024

Editada:

el 9 de Jul. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by