take rows in an array and put them in a table in matlab

To take rows in an array and put them in a table in matlab, you can follow the following steps:

  1. Create a cell array that contains the same number of columns as your original array.
main.m
C = cell(size(A,1), size(A,2));
32 chars
2 lines
  1. Loop through each row in the array and assign it to a cell in the cell array.
main.m
for i = 1:size(A,1)
    C(i,:) = num2cell(A(i,:));
end
55 chars
4 lines
  1. Convert the cell array into a table using the matlab table syntax.
main.m
T = cell2table(C);
19 chars
2 lines
  1. Assign the appropriate column names to the table using the matlab table syntax.
main.m
T.Properties.VariableNames = {'Column1','Column2',...};
56 chars
2 lines

Here is an example code block:

main.m
% Example array
A = [1 2 3; 4 5 6];

% Initialize a cell array
C = cell(size(A,1), size(A,2));

% Loop through each row and assign it to a cell in the cell array
for i = 1:size(A,1)
    C(i,:) = num2cell(A(i,:));
end

% Convert the cell array into a table
T = cell2table(C);

% Assign column names to the table
T.Properties.VariableNames = {'Column1','Column2','Column3'};

% Display the table
T
396 chars
20 lines

gistlibby LogSnag