Reading multiple ascii files and generate 3D matrix

Hello
I have 20 ascii files with a name (A1, A2, A3...A20) each one has 50000 row and 200 column. I am trying to import and read this files and save data in 3D matrix with dimensions (50000*200*20)
I used below code but it is not getting through
Folder = 'D:\My_Files';
Pattern = fullfile(Folder, 'A1');
Files = dir(Pattern);
A = [];
for k = 1 : length(Files)
baseFileName = Files(k).name;
fullFileName = fullfile(Folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data = importdata(fullFilename);
A = cat(3, A, data);
end
when I run code I got error Undefined function or variable 'fullFilename'. Error in New (line 11) data = importdata(fullFilename);

 Accepted Answer

data = importdata(fullFileName);
MATLAB is case-sensitive.

6 Comments

Hello,
Thank you for your reply. Please, I still have a problem it is only read the first file. my file names are(A1, A2, A3...A20)
Pattern = fullfile(Folder, 'A*');
Hello,
Thank you for your reply. Please, it is reading all files now but it is saving only first files to the 3D matrix
Thanks
I suspect that your files do not all store the same size of data.
Folder = 'D:\My_Files';
Pattern = fullfile(Folder, 'A*');
Files = dir(Pattern);
nFiles = length(Files);
A = cell(1, 1, nFiles);
for k = 1 : nFiles
baseFileName = Files(k).name;
fullFileName = fullfile(Folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data = importdata(fullFilename);
A{k} = data;
end
At the end, if all of the files were the same size then you can use
cell2mat(A)
And if the files were not all the same size then you can examine the sizes and figure out what you want to do.
Hello,
Thank you so much for your reply. Please, I checked all files. all has (5520*9). but I still found in my structure. A (1*1*18) cell, Data (1*1 struct). and inside data files Data(5520*9). I still can not find other data. I tried to find the other data but I failed. I used val(:,:,1) and I got error (Undefined function or variable 'val'.)
Folder = 'D:\My_Files';
Pattern = fullfile(Folder, 'A*');
Files = dir(Pattern);
nFiles = length(Files);
A = zeros(5520,9,nFiles);
for k = 1 : nFiles
baseFileName = Files(k).name;
fullFileName = fullfile(Folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data = importdata(fullFilename);
A(:,:,k) = data.Data;
end

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!