subsasgn

Syntax

Description

a(I) = b assigns the values of b into
the elements of a specified by the subscript vector I. b must
have the same number of elements as I or be a scalar
value.

a(I,J) = b assigns the values of b into
the elements of the rectangular submatrix of a specified
by the subscript vectors I and J. b must
have LENGTH(I) rows and LENGTH(J) columns.

A colon used as a subscript, as in a(I,:) = b or a(:,I)
= b indicates the entire column or row.

For multidimensional arrays, a(I,J,K,...) = b assigns b to
the specified elements of a. b must
be length(I)-by-length(J)-by-length(K)-...
or be shiftable to that size by adding or removing singleton dimensions.

a = subsasgn(a,S,b) is called for the syntax a(i)=b, a{i}=b,
or a.i=b when a is an object. S is
a structure array with the following fields:

type — One of the following: '()', '{}',
or '.' specifying the subscript type

For instance, the syntax a(1:2,:) = b calls a=subsasgn(a,S,b) where S is
a 1-by-1 structure with S.type='()' and S.subs
= {1:2,':'}. A colon used as a subscript is passed as ':'.

You can use fixed-point assignment, for example a(:)
= b, to cast a value with one numerictype object
into another numerictype object. This subscripted
assignment statement assigns the value of b into a while
keeping the numerictype object of a.
Subscripted assignment works the same way for integer data types.

Examples

Cast a 16-bit Number into an 8-bit Number

In the first case, a = b replaces a with b while a assumes the value, numerictype object and fimath object associated with b . In the second case, a(:) = b assigns the value of b into a while keeping the numerictype object of a . You can use this to cast a value with one numerictype object into another numerictype object.

Emulate a 40-bit Accumulator of a DSP

This example defines a variable acc to emulate a 40-bit accumulator of a DSP. The products and sums in this example are assigned into the accumulator using the syntax acc(1)=... Assigning values into the accumulator is like storing a value in a register. To begin, turn the logging mode on and define the variables. In this example, n is the number of points in the input data x and output data y , and t represents time. The remaining variables are all defined as fi objects. The input data x is a high-frequency sinusoid added to a low-frequency sinusoid.

The following loop takees a running average of the input x using the coefficients in b . Notice that acc is assigned into acc(1)=... versus using acc=... , which would overwrite and change the data type of acc .

By averaging every other sample, the loop shown above passes the low-frequency sinusoid through and attenuates the high-frequency sinusoid.

plot(t,x,'x-',t,y,'o-')
legend('input data x','output data y')

The log report shows the minimum and maximum logged values and ranges of the variables used. Because acc is assigned into, rather than over written, these logs reflect the accumulated minimum and maximum values.