MATLAB Answers


creating a single structure with 100 entries

Asked by John
on 19 Feb 2012


I was hoping that perhaps somebody could help me out with a problem that I have.

I have imported 100 4x4 matrices into a cell array using the code below.

C = cell(100,1); 
for ii = 1:100 
C{ii} = importdata(['matrix' num2str(ii) '.txt']) ; 

I am wondering how would I create a structure with 100 entries and place a matrix in each entry.

The reason I ask is because I'm trying to get the average of these matrices and this post provides a solution if the matrices are in this format.

Thank you for your help





No products are associated with this question.

1 Answer

Answer by Jiro Doke
on 19 Feb 2012
 Accepted answer

You don't need to create a structure for that. You can do this:

C2 = cat(3, C{:});
avg = mean(C2, 3)

FYI, to answer your original question, the function is cell2struct.

  1 Comment

on 19 Feb 2012

Brilliant, thanks very much!

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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!