size

Class: matlab.io.MatFile
Package: matlab.io

Array dimensions

Syntax

allDims = size(matObj,variable)
[dim1,...,dimN] = size(matObj,variable)
selectedDim = size(matObj,variable,dim)

Description

allDims = size(matObj,variable) returns the size of each dimension of the specified variable in the file corresponding to matObj. Output allDims is a 1-by-m vector, where m = ndims(variable).

[dim1,...,dimN] = size(matObj,variable) returns the sizes of each dimension in separate output variables dim1,...,dimN.

selectedDim = size(matObj,variable,dim) returns the size of the specified dimension.

Tips

  • Do not call size with the syntax size(matObj.variable). This syntax loads the entire contents of the variable into memory. For very large variables, this load operation results in Out of Memory errors.

Input Arguments

matObj

Object created by the matfile function.

variable

String enclosed in single quotation marks that specifies the name of a variable in the MAT-file corresponding to matObj.

dim

Nonzero positive scalar integer that specifies a dimension of the variable.

Output Arguments

allDims

1-by-m vector of sizes of the dimensions of the specified variable, where m = ndims(variable).

dim1,...,dimN

Scalar numeric values, where dimK contains the size of the Kth dimension of variable:

  • If N < ndims(variable), then dimN, equals the product of the sizes of dimensions N through ndims(variable).

  • If N > ndims(variable), the size method returns ones in the output variables corresponding to dimensions ndims(variable)+1 through N.

selectedDim

Scalar numeric value that contains the size of the selected dimension for the specified variable.

Examples

Find the size of the matrix topo in topography.mat without loading any data:

matObj = matfile('topography.mat');
[nrows,ncols] = size(matObj,'topo');

Determine the dimensions of a variable, and process one part of the variable at a time. In this case, calculate and store the average of each column of variable stocks in the example file stocks.mat:

filename = 'stocks.mat';
matObj = matfile(filename);
[nrows, ncols] = size(matObj,'stocks');

avgs = zeros(1,ncols);
for idx = 1:ncols
    avgs(idx) = mean(matObj.stocks(:,idx));
end

Create a three-dimensional array, and call the size method with different numbers of output arguments:

matObj = matfile('temp.mat','Writable',true);
matObj.X = rand(2,3,4);

d = size(matObj,'X')
d2 = size(matObj,'X',2)
[m,n] = size(matObj,'X')
[m1,m2,m3,m4] = size(matObj,'X')

This code returns

d =
     2     3     4

d2 =
     3

m =
     2
n =
    12

m1 =
     2
m2 =
     3
m3 =
     4
m4 =
     1

See Also

|

Was this topic helpful?