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에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!