Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

open .mat files and concatenate their matrix contents

Asked by James on 29 Jun 2012


I will like to open many .mat files (sequentially named file_0.mat, file_1.mat,.....file_50.mat), and then concatenate (vertically and sequentially) the matrix data (about 2 million rows by 50 columns) into one variable available in the workspace accessible to other programs.

How can I write matlab codes to carry out this processes?






No products are associated with this question.

1 Answer

Answer by Walter Roberson on 29 Jun 2012
Accepted answer


Walter Roberson on 29 Jun 2012

matData = [matData; matLoad.(num2str(k, 'mut%d'))];

James Tursa on 29 Jun 2012

IMO that is not a good way to do this in general. At every iteration in the loop you need to copy all of the data to a new memory block to do the concatenation. This drags performance. What I would do is read each iteration into an element of a cell array. Then after all of the files have been read concatenate all of the cell array contents into the big matrix. That way the data only gets copies once, not many times during the file reading iterations.

James on 30 Jun 2012

Thanks Walter. It worked! I appreciate James' comment about efficiency, and I tried to implement the codes as follows:

 matData = [];
 for k = 0:2
 matFilename = sprintf('mut%d.mat',k);
 matLoad = load(matFilename);
 similar = cell(size(matLoad.(num2str(k, 'mut%d'))));
 matData = [matData; similar];

But, I'm not sure if this what you meant.


Walter Roberson

Contact us