How to read data of files which are in the same folder
古いコメントを表示
Hi all,
I've been searching on the internet without any succeed...
- I want to read files which are in a directory and then read data contaiedn in these files (there are columns).
- Below is my script :
cd 'C:\Users\'
folder=('C:\Users\')
txt=dir(fullfile('dry','*.txt'))
% creation of a matrice that contains paths of files
for i=1:length(txt)
fileName(i,:)=fullfile(folder,txt(i).name)
end
% reading of data which is contained in these files
for i=1:length(fileName)
M(i,:)=importdata(fileName(i,:))
end
Very easy but it doesn't work ... It says : "Subscripted assignment between dissimilar structures."
Thanks for your help ! Florian
採用された回答
その他の回答 (1 件)
dpb
2014 年 3 月 25 日
Firstly, have you checked size() txt after the dir call? You don't want fullfile here --
>> fullfile('dry','*.txt')
ans =
dry\*.txt
>>
fullfile presumes the first argument is a directory and adds the delimiter. I've always thought it needed some more flexibility, but it is what it is...
Try sotoo--
folder=('C:\Users')
d=dir(fullfile(folder,'dry*.txt'));
for i=1:length(d)
M(i,:)=importdata(d.name(i));
end
No need for the intermediary; just use the name field directly. It's one real advantage of cellstrings over character variables--you don't need the ':' indexing to get the full string.
Now your at least down to whether the data is of the right form and all...
2 件のコメント
Florian
2014 年 3 月 25 日
dpb
2014 年 3 月 25 日
fullfile concatenates the pieces you give it--
>> folder=('C:\Users');
(fullfile(folder,'dry*.txt'))
ans =
C:\Users\dry*.txt
>>
That's a fully-qualified path and wildcard filename to pass to dir Presuming that's where your files are, that's what you need for it.
But, indeed the .name field returned by dir doesn't include the path so you do need to prepend it again--my bad for forgetting/overlooking the point.
And, you're also correct in that it's d that's the structure array to reference into inside the loop.
So
M(i,:)=importdata(fullfile(folder,d(i).name));
looks like it should be correct (if I've not blown something else... :) )
カテゴリ
ヘルプ センター および File Exchange で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!