1
votes

here's my matlab problem. I need to write a matlab function that copies all data from a struct datatype to a matrix. Problem is that each entry can have a different length. So what I've been doing so far is iterating through all these entries in order to precalculate the final size of the matrix.

%Calculate final size of Matrix
nFieldsY = length(CompleteData.Y);
nFieldsX = length(CompleteData.X);
maxRowNumber = 0;
maxColNumber = nFieldsY + nFieldsX;

for j = 1:nFieldsY
  l_x = length (CompleteData.X(1,j).Data);
  l_y = length (CompleteData.Y(1,j).Data);
  compAr = [maxRowNumber l_x l_y];
  maxRowNumber = max(compAr);
end

ResultMatrix = zeros(maxRowNumber, maxColNumber);

So "ResultMatrix" represents the maximum of data that could possibly be stored. Now I would like to replace the first n entries in column m of the matrix. The rest of the column should still be filled with zeros. Despite all my efforts I get the "dimension mismatch" error.

Appreciate any help. Thanks

1

1 Answers

3
votes

Something like this

ResultMatrix(1:n,m) = n_entries

where n_entries is a n x 1 matrix of the values you want to put in the column.

The 1:n picks out rows 1 to n of ResultMatrix, and the m picks column m.