image thumbnail

ndarray2table(arr, dimnames, dimvalues, ascategorical)

version (4.28 KB) by Matthias Treder
Converts a multi-dimensional array into at Matlab table


Updated 27 Feb 2018

View License

What it does:
Convert an n-dimensional array into a table. The first dimension defines the row dimension of the table, the other dimensions are pasted as additional rows underneath. To code the dimensions, an additional column is created for each dimension. The values in the array are stored in the column "Value".

Scientific data often comes as multi-dimensional arrays (nd-array). For beautiful plots, many people resort to R (using ggplot2) or Python (using Pandas and matplotlib/seaborn) but here the input should be tabular data in form of a dataframe.
Reshaping the nd-array into a dataframe in R or Python can be messy (been there). It is easier to bring it into the right format in Matlab first by turning it into a Matlab table. This table can then be exported as a CSV file using writetable(). It is easy to import CSV files as a dataframe in R or Python.
Input arguments:
arr - [s1 x s2 x ... x sd] multidimensional array of N dimensions
dimnames (optional) - cell array containing the label for of the N dimensions
dimvalues (optional) - normally each dimension is numbered as 1 : size of dimension. (optional)
If the values have different meanings a cell array can be provided

% Suppose we measured 10 samples of data. From each sample, we measured
% 3 different properties, namely height, weight, and volume (second dimension in the array). Furthermore,
% we measured these properties at 8 different time points (third dimension) and 2 different locations (fourth dimension).
% Our 4d-array thus has the dimensions [samples x properties x time x location]
X = randn(10, 3, 8, 2);

% We first give a name to each dimension. We then turn it into a table providing the dimnames as argument
dimnames = {'Sample' 'Property' 'Time' 'Location'};
T = ndarray2table( X, dimnames);

% Show first 10 elements of table

% By default, ndarray2table codes each slide of a dimension numerically.
% The first slice is coded as 1, the second as 2, and so on.
% For the dimensions 'Property' and 'Location', these codes are not so informative.
% To make the table more readable, we like to use string values instead of these codes.
% To this end, we create a NESTED cell array "values" that will
% provide the values for each dimension. The number of values must
% correspond to the size of the dimension.
% Note that we are happy with the default numerical coding of 'Sample' and 'Time',
% so we leave the 1st and 3rd element of the values array empty.

dimvalues = cell(4, 1);
dimvalues{2} = {'height' 'weight' 'volume'}; % Values of 'Property'
dimvalues{4} = {'Paris' 'Berlin'}; % Values of 'Location'

% Now we're ready to create the table and pass dimvalues as an additional parameter
T = ndarray2table( X, dimnames, dimvalues);

% Show first 10 elements of table

% Now you can look at T in the variable viewer to make sure the format is
% as desired. Finally, we save the table as CSV file which can be easily
% read in in R or Python.
writetable(T, 'mytable.csv')

Cite As

Matthias Treder (2021). ndarray2table(arr, dimnames, dimvalues, ascategorical) (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!