Why can't I use a variable to put all .txt files in a directory into an array?

1 visualización (últimos 30 días)
EL
EL el 9 de Sept. de 2019
Comentada: Adam Danz el 10 de Sept. de 2019
Hllo!
I'm trying to use the following code to make an array of all .txt files in a specified directory to be later used by another script.
Here's the script that I'm working on
clc
clear
clear all
rootfolder = 'T:\Data\';
Chopped = '\Chopped';
prompt = 'Enter date of experiment to be analyzed: ';
DataDate = input(prompt, 's');
Directory = strcat(rootfolder,DataDate,Chopped);
cd(Directory);
FilesToRead=dir;
for K = 1 : length(FilesToRead)
thisfilename = FilesToRead(K).name; %just the name
%read the file data
%do something with the data
end
I have 18 datafiles in the folder I'm working with.
I'm getting a 20x1 structure. 18 of the data points are after '.' and '..'.
If I change k=1:length(FilesToRead) to 3:length(FilesToRead), I get no change in results.
when I change
FilesToRead=dir;
to
FilesToRead=dir(*.txt)
I get a 0x1 structure with nothing.
dir x00.txt
I get an output of the answer.
Following the MathWorks suggestion of pulling out folders with specific extensions,
List all files with a .m extension that contain the term my.
Create a folder, myfolder, that contains the files myfile1.m, myfile2.m, and myfile3.txt.
mkdir myfolder
movefile myfile1.m myfolder
movefile myfile2.m myfolder
movefile myfile3.txt myfolder
List the matching files in myfolder.
cd myfolder
dir *my*.m
myfile1.m myfile2.m
When I type in
cd(Directory);
I get the correct directory
If I type in dir x000.txt,
I get the correct file name output.
if I type in
FilesToRead=dir;
I get the correct answer of all data files in the folder, as well as '.' and '..'
If I type in
dir *x*.txt
I get no results.
Variables are not working and I don't know why. This is the exact same format as mathworks.
I'm lost

Respuestas (2)

Walter Roberson
Walter Roberson el 9 de Sept. de 2019
rootfolder = 'T:\Data';
Chopped = 'Chopped';
prompt = 'Enter date of experiment to be analyzed: ';
DataDate = input(prompt, 's');
Directory = fullfile(rootfolder, DataDate, Chopped);
FilesToRead = dir(Directory);
FilesToRead([filesToRead.isdir]) = [];
filenames = fullfile(Directory, {FilesToRead.name});
for K = 1 : length(filenames)
thisfilename = filenames{K}; %fully qualified filename
%read the file data
%do something with the data
end

Adam Danz
Adam Danz el 10 de Sept. de 2019
Editada: Adam Danz el 10 de Sept. de 2019
Here's a bunch of feedback.
1) don't use "clear all". Here's why: link.
clc
clear
% clear all % avoid doing this.
2) instead of relying on the user to enter the correct date in the correct format in hopes that it leads to an existing path, use the uigetdir() function which will allow the user to select the directory directly.
rootfolder = 'T:\Data\';
Chopped = '\Chopped';
prompt = 'Enter date of experiment to be analyzed: ';
DataDate = input(prompt, 's');
Directory = strcat(rootfolder,DataDate,Chopped);
% Instead,
Directory = uigetdir(rootfolder);
3) don't change the CD. Instead, specify the directory in the dir() input. (see Walter's answer to select relevant files)
cd(Directory);
FilesToRead=dir;
% Instead,
FilesToRead = dir(Directory);
4) Skip elements in "FilesToRead" that aren't files. (see Walter's answer to select relevant files)
for K = 1 : length(FilesToRead)
if FilesToRead(k).isdir % % % % % % % % % % %
continue % ADD THIS %
end % % % % % % % % % % %
thisfilename = FilesToRead(K).name; %just the name
%read the file data
%do something with the data
end
  4 comentarios
EL
EL el 10 de Sept. de 2019
Uigetdir needs a gui to operate. I'm ssh'ing to a linux system and processing data renotely through command line. Besides, the date is a simple ddmmyyyy format to select he correct folder. I won't mess it up.
Adam Danz
Adam Danz el 10 de Sept. de 2019
ah, OK then. You could use exist(Directory ,'dir') to confirm that the directory exists or check if the dir output is empty.
FilesToRead = dir(Directory);
if isempty(FilesToRead)
error('Directory does not exist: %s', Directory)
end

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by