Readall files with different size,format , and extension in a directory

1 回表示 (過去 30 日間)
Mini Me
Mini Me 2014 年 4 月 18 日
コメント済み: Mini Me 2014 年 4 月 18 日
Hello,
I am trying to read 21 files that is in my directory. Some of the files are *a.txt and others *a.DAT files. The *a.DAT files has $ included in the data and are comma delimited and the *.txt files are comma delimited..This is the code I'm using below:
fnames = dir('C:\Program Files\MATLAB71\work\subfolder\files location);
tradata = cell(1,length(fnames));
for k=1:length(fnames)
fname = fnames(k).name;
tradata{k} = dlmread(fname, ',', '%e%c%f%*$';
End
Each time I run the code it finds 23 files in the dir instead of 21. When I look inside fname the first two files are:. And .. so when it gets too trada{k} it throws an error saying No such file in directory. Why does it see those files '.'and '..'. it doesn't make sense. So when I change the full directory to a specific type. Lets say *a.txt. it see all the *a.txt files but won't read them because when it call text text read it throws an error. Saying the range(l)=0 so there's no range(l)=1. I don't understand, help!!!

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 18 日
folder='C:\Program Files\MATLAB71\work\subfolder\files location'
fnames = [dir([folder '\*.dat']);dir([folder '\*.txt'])];
  6 件のコメント
Mini Me
Mini Me 2014 年 4 月 18 日
@Azzi So I decided to change the dlmread line by Tradata{k}=load(fname(k,1), ',') Error: unexpected text in line number 1. :'$'
Any help?
Mini Me
Mini Me 2014 年 4 月 18 日
Size of fnames is 21 1 which is #files in directory Size of fname is 1 17 which I think is the number of letters in the file name

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by