Using rows of different files to create a matrix

1 view (last 30 days)
I have 6 separate files all within the workspace each with 3 columns of data with an unknown number of rows but equal for all files. I am trying to use the first row of each file as a line in a new matrix i.e end up with a 6*3 matrix. I want to repeat this for every row until it ends.
So the matrix looks like: row 1 of file 1 row 1 of file 2 row 1 of file 3 row 1 of file 4 row 1 of file 5 row 1 of file 6
Does this make sense? Does anyone know how to do this?

Answers (1)

Jos (10584)
Jos (10584) on 27 Feb 2018
% Load in the data and store in cells
for k=1:6,
filename = ...
C{k} = MyLoadFile(filename) ; % C{k} is a N-by-3 matrix
end
D = cat(2,C{:}) % D is a N-by-18 matrix, holding all values
% D(k,:) is the data for all first rows of the 6 files
E = arrayfun(@(k) reshape(D(k,:),3,6).', 1:size(D,1),'un',0)
% E{k} holds the requested 6-by-3 matrix for the k-th row

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!