How to insert a n x m matrix or table, into an existing table?

Illustration
Shrishtisharma - 2020-07-28T13:26:43+00:00
Question: How to insert a n x m matrix or table, into an existing table?

I would like to nest an n x m matrix into a table, by using a for loop to populate each row of the table with a corresponding matrix. I am running matlab 2018a. Below you can find example code:   % create the table subject = [1; 2; 3; 4]; condition = {'cond1';'cond2';'cond3';'cond4'}; data = NaN(4,1); T = table(subject,condition,data); % populate the table with matrixes, using a for loop for i = 1:1:size(T,1) data = rand(10,5); T.data(i) = data; % ERROR Unable to perform assignment because the left and right sides have a different number of elements. end I thought it was possible to create nested tables. However, I am not sure, on account of the error. I know tables within tables exists, but I am unsure whether you can nest a matrix/array in a table. Therefore, it is also allowed to transform the data matrix to a table, and then nest the table in the table. Of importance, I want to retain the for loop mechanism to populate the table.

Expert Answer

Profile picture of John Michell John Michell answered . 2025-11-20

Sounds like data should be a cell array to which you insert 10x5 matrices.
replace:
 
data = cell(4,1);

And

T.data{i} = data; 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!