Programmers learn & share
56 views

Recently, I have started working in MATLAB and I came to know that this software has got some fantastic features to build any type of code. Still, there are specific rules that must be followed first, for calling a function indirectly. Many times, we work on building an efficient code, but it does not run. After spending quite a time debugging, we come to know that the error occurred is so fundamental in a level like either about case sensitivity or we forget to use the correct syntax, improper use of function name or function’s directory.

One of the advance errors in MATLAB is "Undefined operator for input arguments of type 'function_handle'.” The primary example of function calling is given below, For example:

A function handle is a MATLAB value that provides a means of calling a function indirectly. You can pass function handles in calls to other functions (often called function functions). You can also store function handles in data structures for later use (for example, as Handle Graphics callbacks). A function handle is one of the standard MATLAB data types, i.e.,

handle = @function name returns a handle to the specified MATLAB function.

At the time you create a function handle, the function you specify must be on the MATLAB path and in the current scope of the code creating the handle. For example, you can create a handle to a local function as long as you do so from within the file that defines that local function. This condition does not apply when you evaluate the function handle. You can, for example, execute a local function from a separate (out-of-scope) file using a function handle. This requires that the handle was created by the local function (in-scope).

handle = @(arglist)anonymous_function constructs an anonymous function and returns a handle to that function. The body of the function, to the right of the parentheses, is a single MATLAB statement or command. arglist is a comma-separated list of input arguments. Execute the function by calling it by means of the function handle, handle.

Constructing a Handle to a Named Function

The following example creates a function handle for the humps function and assigns it to the variable fhandle.

fhandle = @humps;

Pass the handle to another function in the same way you would pass any argument. This example passes the function handle just created to fminbnd, which then minimizes over the interval [0.3, 1].

>> x = fminbnd(fhandle, 0.3, 1)

>> x =

    0.6370

The fminbnd function evaluates the @humps function handle. A small portion of the fminbnd file is shown below. In line 1, the funfcn input parameter receives the function handle @humps that was passed in. The statement, in line 113, evaluates the handle.

1   >> function [xf, fval, exitflag, output] = ...

         fminbnd(funfcn,ax,bx,options,varargin)

            .

            .

            .

113 >> fx = funfcn(x,varargin{:});

The above example can easily resolve the error of function handling, which are not directly called, there must be the same directory where the function file is saved and also the location of other file in which that function is called.

posted Mar 31 in other by (100 points)   | 56 views

You need to give ‘ymax’ an argument, so it returns a value.

This works:

ymax= @(xx) 5.*sin(pi.*xx./10);
h = 1;
y=[0:h:ymax(2)];

1. Arrays and Array Operations

1.1 Simple Arrays

Using the [] operator

>> x=[0 .1*pi .2*pi .3*pi .4*pi .6*pi .7*pi .8*pi .9*pi pi] %create an array with inserting z values between square brackets

x =

0    0.3142    0.6283    0.9425    1.2566    1.8850    2.1991    2.5133    2.8274    3.1416

 

>> y=sin(x) %MATLAB understands we want to compute the sin of each value in x, so it calculates it and save all values to y

y =

0    0.3090    0.5878    0.8090    0.9511    0.9511    0.8090    0.5878    0.3090    0.0000

 

1.2 Array addressing or indexing

Using the () operator

>> x(3) %means 3rd element in x; note that number must be real positive or logical

ans =

0.6283

>> y(1:5) %1st to 5th values

ans =

0    0.3090    0.5878    0.8090    0.9511

>> x(7:end) %all elements from the 7th to the end

ans =

2.1991    2.5133    2.8274    3.1416

 

>> y(3:-1:1) %start with 3rd element, decrease by 1, till 1st value

ans =

0.5878    0.3090         0

 

>> x(2:2:7) %start with 2nd element, increment by 2, till 7th value

ans =

 

0.3142    0.9425    1.8850

 

>> y([8 8 2 9 1]) %8th, 8th, 2nd, 9th and 1st value

ans =

0.5878    0.5878    0.3090    0.3090         0

2,218 questions
2,683 answers
59 comments
241 users