Info

This question is closed. Reopen it to edit or answer.

How to creat a matrix by choosing a particular array from 100 data files.

3 views (last 30 days)
Hi! I have 100 data files with two arrays (e.g., a_1_0, a_1_1, a_1_2, a_1_3...a_1_100.). I want to creat a matrix by choosing second array out of all 100 files, and then perform some operation.
  2 Comments
TADA
TADA on 20 Nov 2018
I Don't Understand How The data Is Saved, Could You Post A Sample Data File?
Abhishek Singh
Abhishek Singh on 20 Nov 2018
Hi there,
All of them are .dat files. So there are 100 files with the dimesnions 4000 X 2 each. I need to creat a matrix choosing the second array from each file. So the final matrix will be 4000 X 100.

Answers (1)

TADA
TADA on 21 Nov 2018
Assuming those are text files formatted like you mentioned above a_x_y
you can read the second array using text scan:
xCell = textscan('text or file id', '%*s %*n %n', 'Delimiter', '_');
So now you can read the files in a loop:
folderPath = pwd;
% I assume your files are saved in a specific folder
% In that case, you can get the list with dir function
files = dir([folderPath '\*.dat']);
% preallocate matrix
A = zeros(5,length(files));
for i = 1:length(files)
fid = fopen([files(i).folder '\' files(i).name]);
if fid < 0
continue;
end
% read text file, assuming the format is like you wrote a_x_y
% This will read only the second column vector from the file
xCell = textscan(fid, '%*s %*n %n', 'Delimiter', '_');
fclose(fid);
% unravel text fields cell araray
x = xCell{:};
% If you can read just the second
A(:, i) = x;
end
if you have parallel computing toolbox, reading 100 files is a classic task a parfor loop

This question is closed.

Community Treasure Hunt

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

Start Hunting!