MATLAB Answers

## deleting and adapting columns in matrix of function handles

Asked by Koen

### Koen (view profile)

on 29 Nov 2015
Latest activity Answered by dpb

### dpb (view profile)

on 29 Nov 2015
Hello,
If you a matrix that contains function handles, so for example:
A = @(x) [x , x^2, x^3; 2*x, 2*x^2, 2*x^3]
How can I delete/adapt the second column of A ?

#### 0 Comments

Sign in to comment.

## 1 Answer

Answer by dpb

### dpb (view profile)

on 29 Nov 2015

There are no "columns" in A and A is not a matrix...
>> A = @(x) [x , x^2, x^3; 2*x, 2*x^2, 2*x^3]
A =
@(x)[x,x^2,x^3;2*x,2*x^2,2*x^3]
>> whos A
Name Size Bytes Class Attributes
A 1x1 16 function_handle
>> func2str(A)
ans =
@(x)[x,x^2,x^3;2*x,2*x^2,2*x^3]
>> whos ans
Name Size Bytes Class Attributes
ans 1x31 62 char
>>
As shown, A is a single function handle despite it defining a function that returns a 2D matrix when executed.
You either create a new function handle as desired or, in a more roundabout way, convert to a string representation and edit on the string and then redefine the handle from that new string via func2str
You can, of course, create an array of function handles and treat each element within as the above single handle and rewrite individual elements inside such. NB: however, they must be cell arrays.
>> B=[@(x) 1, @(x) x, @(x) x^2]
Error using horzcat
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
>> B={@(x) 1, @(x) x, @(x) x^2}
B =
@(x)1 @(x)x @(x)x^2
>> whos B
Name Size Bytes Class Attributes
B 1x3 228 cell
>>
Also note, now the array is simply a cell array, just so happens the cell content is a set of function handles.

#### 0 Comments

Sign in to comment.