Main Content

getMapData

Retrieve data from map layers

Description

example

mapData = getMapData(map) returns a cell array of matrices for the data in each layer of the specified multiLayerMap object. For binary or occupancy map layers, the values of this function are passed to the getOccupancy function. mapData is returned as an cell-array of matrices for each layer.

mapData = getMapData(map,layername) returns all the map data for the specified layer name. mapData is returned as a matrix equal to the DataSize of the specified layer.

mapData = getMapData(map,layername,layerinputs) takes the layerinputs arguments and passes them to the getMapData object function for the specified map layer name. To access individual cells or blocks of data in the world, local, or grid coordinates, see the syntaxes of getMapData.

Examples

collapse all

The multiLayerMap object enables you to group multiple map layers and define behavior for those layers when setting and getting data. Using separate map layers, you can store various map data and specify different behaviors for each. You can also define the SetTransformFcn and GetTransformFcn function handles for a map layer so that dependencies are created between layers. This example shows how to store data in a map layer and implement event listeners which update other maps. These maps store how many times the data is updated or accessed.

Dependent Layers

Create two independent map layers.

mapAccessed = mapLayer(zeros(10,10),'LayerName','GetListener');
mapModified = mapLayer(zeros(10,10),'LayerName','SetListener');

Specify function handles for the get and set transform functions used in the main map layer. These functions increment the value of a grid location when you get or set map data in the input map mainMap. See Listener Function Handles for the function implementation.

setHookFcn = @(mainMap,values,varargin)exampleHelperSetHookFcn(mapModified,mainMap,values,varargin{:});
getHookFcn = @(mainMap,values,varargin)exampleHelperGetHookFcn(mapAccessed,mainMap,values,varargin{:});

Create the main map layer with default values of 0.5. Specify the function handles to create the layer depencies.

map = mapLayer(repmat(0.5,10,10),...
                'GetTransformFcn',getHookFcn,...
                'SetTransformFcn',setHookFcn);

Add all maps to the same multiLayerMap object.

mapLayers = multiLayerMap({map,mapAccessed,mapModified});

Set the (0,0) map location with a value of zero using the setMapData object function of map.

setMapData(map,[0 0],0)

Check that mapModified incremented their value.

getMapData(mapModified,[0 0])
ans = 1

Get the data you just set to the main map layer. The expected value of zero is returned.

getMapData(map,[0 0])
ans = 0

Check that mapAccessed incremented their value.

getMapData(mapAccessed,[0 0])
ans = 1

Update the entire map with a matrix of values. Access the data as well.

setMapData(map,rand(10,10))
getMapData(map)
ans = 10×10

    0.6443    0.2077    0.3111    0.5949    0.0855    0.9631    0.0377    0.1068    0.0305    0.1829
    0.3786    0.3012    0.9234    0.2622    0.2625    0.5468    0.8852    0.6538    0.7441    0.2399
    0.8116    0.4709    0.4302    0.6028    0.8010    0.5211    0.9133    0.4942    0.5000    0.8865
    0.5328    0.2305    0.1848    0.7112    0.0292    0.2316    0.7962    0.7791    0.4799    0.0287
    0.3507    0.8443    0.9049    0.2217    0.9289    0.4889    0.0987    0.7150    0.9047    0.4899
    0.9390    0.1948    0.9797    0.1174    0.7303    0.6241    0.2619    0.9037    0.6099    0.1679
    0.8759    0.2259    0.4389    0.2967    0.4886    0.6791    0.3354    0.8909    0.6177    0.9787
    0.5502    0.1707    0.1111    0.3188    0.5785    0.3955    0.6797    0.3342    0.8594    0.7127
    0.6225    0.2277    0.2581    0.4242    0.2373    0.3674    0.1366    0.6987    0.8055    0.5005
    0.5870    0.4357    0.4087    0.5079    0.4588    0.9880    0.7212    0.1978    0.5767    0.4711

Check that mapModified and mapAccessed incremented their values.

getMapData(mapModified)
ans = 10×10

     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     2     1     1     1     1     1     1     1     1     1

getMapData(mapAccessed)
ans = 10×10

     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     2     1     1     1     1     1     1     1     1     1

The bottom-left location returns two and all other values are one. This confirms the listener functions are working as intended.

Listener Function Handles

These functions implement the get and set example helper functions that update the other map layers.

function valuesOut = exampleHelperSetHookFcn(modifiedMap,sourceLayer,valueIn,varargin)
    % Pass output through
    valuesOut = valueIn;
    
    % If no additional inputs are passed, return immediately.
    if numel(varargin) == 0
        return;
    else
        % Otherwise, increment the value in the modifiedMap.
        if numel(varargin) == 1
            currentValue = getMapData(modifiedMap);
            setMapData(modifiedMap,currentValue+1);
        else        
            currentValue = getMapData(modifiedMap,varargin{1},varargin{3:end});
            % setMapData syntax <<<<>>>>
            setMapData(modifiedMap,varargin{1},currentValue+1,varargin{3:end});
        end
    end
end

function data = exampleHelperGetHookFcn(accessedMap,sourceLayer,valuesIn,varargin)
    
    data = valuesIn;

    % If no additional inputs are passed, check if the values in
    if numel(varargin) == 0
        if isequal(size(valuesIn),sourceLayer.DataSize)
            % Increment the depedent map.
            currentValue = getMapData(accessedMap);
            setMapData(accessedMap,currentValue+1);
        end
    else
        currentValue = getMapData(accessedMap,varargin{:});
        setMapData(accessedMap,varargin{1},currentValue+1,varargin{3:end});
    end
end

Input Arguments

collapse all

Multilayer map, specified as a multiLayerMap object.

Map layer name, specified as a string scalar or character array. Map layers have their name specified when creating the multiLayerMap object.

Variable-length inputs to getMapData function of map layer, specified as varargin. To specify individual cells or blocks of data in the world, local, or grid coordinates, see the syntaxes of getMapData.

Output Arguments

collapse all

Data values from map layer, returned as a matrix. By default, the function returns all data on the layer as an M-by-N-by-DataDims matrix. M and N are the grid height and width respectively. DataDims are the dimensions of the map data, map.DataSize(3,:).

For other syntaxes, the map data may be given as an array of values with size N-by-DataDims or as a subregion of the full matrix.

Introduced in R2021a