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)
Show older comments
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
Answers (1)
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
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!