Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Одиноков / МиИПиС_асп_13г / MATLAB_R2013a_Выбор.doc
Скачиваний:
32
Добавлен:
15.04.2015
Размер:
4.03 Mб
Скачать

Comparing Handles to Nested Functions

MATLAB considers function handles to the same nested function to be equal only if your code constructs these handles on the same call to the function containing the nested functions. Given this function that constructs two handles to the same nested function:

function [h1, h2] = test_eq(a, b, c)

h1 = @findZ;

h2 = @findZ;

function z = findZ

z = a.^3 + b.^2 + c';

end

end

function handles constructed from the same nested function and on the same call to the parent function are considered equal:

[h1 h2] = test_eq(4, 19, -7);

isequal(h1, h2),

ans =

1

while those constructed from different calls are not considered equal:

[q1 q2] = test_eq(3, -1, 2);

isequal(h1, q1)

ans =

0

Comparing Handles Saved to a mat-File

If you save equivalent anonymous or nested function handles to separate MAT-files, and then load them back into the MATLAB workspace, they are no longer equal. This is because saving the function handle loses track of the original circumstances under which the function handle was created. Reloading it results in a function handle that compares as being unequal to the original function handle.

Create two equivalent anonymous function handles:

h1 = @(x) sin(x);

h2 = h1;

isequal(h1, h2)

ans =

1

Save each to a different MAT-file:

save fname1 h1;

save fname2 h2;

Clear the MATLAB workspace, and then load the function handles back into the workspace:

clear all

load fname1

load fname2

The function handles are no longer considered equal:

isequal(h1, h2)

ans =

0

Note, however, that equal anonymous and nested function handles that you save to the same MAT-file are equal when loaded back into MATLAB.

  • R2013a>MATLAB>Language Fundamentals>Data Types>Map Containers

  • Overview of the Map Data Structure

A Map is a type of fast key lookup data structure that offers a flexible means of indexing into its individual elements. Unlike most array data structures in the MATLAB® software that only allow access to the elements by means of integer indices, the indices for a Map can be nearly any scalar numeric value or a character string.

Indices into the elements of a Map are called keys. These keys, along with the data values associated with them, are stored within the Map. Each entry of a Map contains exactly one unique key and its corresponding value. Indexing into the Map of rainfall statistics shown below with a string representing the month of August yields the value internally associated with that month, 37.3.

Mean monthly rainfall statistics (mm)

Keys are not restricted to integers as they are with other arrays. Specifically, a key may be any of the following types:

  • 1-by-N character array

  • Scalar real double or single

  • Signed or unsigned scalar integer

The values stored in a Map can be of any type. This includes arrays of numeric values, structures, cells, strings, objects, or other Maps.

Note:   A Map is most memory efficient when the data stored in it is a scalar number or a character array.

  • R2013a>MATLAB>Language FundamentalsData Types>Map Containers

  • Description of the Map Class

On this page…

Properties of the Map Class

Methods of the Map Class

A Map is actually an object, or instance, of a MATLAB® class called Map. It is also a handle object and, as such, it behaves like any other MATLAB handle object. This section gives a brief overview of the Map class. For more details, see the containers.Map reference page.