How to read data of files which are in the same folder

37 views (last 30 days)
Hi all,
I've been searching on the internet without any succeed...
  1. I want to read files which are in a directory and then read data contaiedn in these files (there are columns).
  2. 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

Accepted Answer

Florian
Florian on 25 Mar 2014
Many thanks it works !
But so sorry, there's a new error I'm trying to fix from an hour. Because of a header, I've coded as below : delimiterIn=' '; headerLines=11; M=[] ;
for i=1:length(txt)
A=importdata(txt(i).name,delimiterIn,headerLines);
M=[M A];
end
Then I'm trying to view data by writing M.data(1,1) for instance and then I get this message : "Field reference for multiple structure elements that is followed by more reference blocks is an error."
Thank you so much for your help ! Florian
  6 Comments
Florian
Florian on 26 Mar 2014
Edited: Florian on 26 Mar 2014
Hi,
I copy the code that I've written, it was finally easier with textscan, because my goal was to build a matrix of matrix and with importdata, because it's a structure, it seems being impossible.
txt=dir(fullfile(folder,'*.txt'));
for i=1:length(txt)
block = 1
fid=fopen(txt(i).name)
inputText=textscan(fid,'%s',11,'delimiter','\n') % number of lines of header
text=inputText{1} %stocke le header
while (~feof(fid))
inputText=textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f\n') % each '%f' corresponds to a column
data{i,block,1}=cell2mat(inputText)
block = block + 1
end
fclose(fid)
end
That way I've succeeded in building a cell like data{nb_of_files,1}(row,column) I can access more easily to my data !
Thanks Florian
Image Analyst
Image Analyst on 26 Mar 2014
Please mark dpb's answer ( not yours) as "Accepted" .

Sign in to comment.

More Answers (1)

dpb
dpb on 25 Mar 2014
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 Comments
Florian
Florian on 25 Mar 2014
Hi ! Thank you very much for you answer ! But there's still something wrong with the fullfile function. It doesn't "create" the entire path to the file, it just take into account the name and the extension of the file. Though when I use it into the loop, necessarily I have the answer "Unable to open file."
Do you have an idea ?
And in the loop I think it's rather :
for i=1:length(d)
M(i,:)=importdata(d(i).name);
end
dpb
dpb on 25 Mar 2014
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... :) )

Sign in to comment.

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!