Why can't I use a variable to put all .txt files in a directory into an array?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
1 commentaire
Walter Roberson
le 9 Sep 2019
FilesToRead=dir('*.txt');
Provided that there are .txt files in that directory.
Réponses (2)
Walter Roberson
le 9 Sep 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
0 commentaires
Adam Danz
le 10 Sep 2019
Modifié(e) : Adam Danz
le 10 Sep 2019
Here's a bunch of feedback.
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 commentaires
Adam Danz
le 10 Sep 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
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!