Programming |
How to Use the Comma-Separated List
Common uses for comma-separated lists are
The following sections provide examples of using comma-separated lists with cell arrays. Each of these examples applies to MATLAB structures as well.
Constructing Arrays
You can use a comma-separated list to enter a series of elements when constructing a matrix or array. Note what happens when you insert a list of elements as opposed to adding the cell itself.
When you specify a list of elements with C{:, 5}
, MATLAB inserts the four individual elements:
When you specify the C
cell itself, MATLAB inserts the entire cell array:
Displaying Arrays
Use a list to display all or part of a structure or cell array:
Concatenation
Putting a comma-separated list inside square brackets extracts the specified elements from the list and concatenates them:
Function Call Arguments
When writing the code for a function call, you enter the input arguments as a list with each argument separated by a comma. If you have these arguments stored in a structure or cell array, then you can generate all or part of the argument list from the structure or cell array instead. This can be especially useful when passing in variable numbers of arguments.
This example passes several attribute-value arguments to the plot
function:
X = -pi:pi/10:pi; Y = tan(sin(X)) - sin(tan(X)); C{1,1} = 'LineWidth'; C{2,1} = 2; C{1,2} = 'MarkerEdgeColor'; C{2,2} = 'k'; C{1,3} = 'MarkerFaceColor'; C{2,3} = 'g'; plot(X, Y, '--rs', C{:})
Function Return Values
MATLAB functions can also return more than one value to the caller. These values are returned in a list with each value separated by a comma. Instead of listing each return value, you can use a comma-separated list with a structure or cell array. This becomes more useful for those functions that have variable numbers of return values.
This example returns four values to a cell array:
C = cell(1, 4); [C{:}] = fileparts('work/mytests/strArrays.mat') C = 'work/mytests' 'strArrays' '.mat' ''
Generating a List from a Structure | Fast Fourier Transform Example |
© 1994-2005 The MathWorks, Inc.