Dot indexing is not supported for variables of this type. For an array in an object

5 views (last 30 days)
Hi, i'm trying to make a method in a object which receives an array and the object itself as input arguments and uses a property as indices for the array that is used as input. But I get the prompt that dot indexing is not supported for variables of this type. I have tried this outside of an object so this function should work but it doesn't, it probably has something to do with the self.currenGridCell syntax but I do not understand how I can fix this. I am new to OOP so all or any help is welcome.
classdef Herd
properties
name;
nCows{mustBePositive, mustBeInteger};
currentGridCell(1,2);
end
methods
function newHerd = Herd(name, nCows, currentGridCell)
if nargin == 3 % if all three arguments (r, p, a) are given to the Constructor
newHerd.name = name;
newHerd.nCows = nCows;
newHerd.currentGridCell = currentGridCell;
end
end
function grasslands = eat(grasslands, self)
grasslands(self.currentGridCell(1,1), self.currentGridCell(1,2)) = grasslands(self.currentGridCell(1,1), self.currentGridCell(1,2)) - (self.nCows*30);
end
end
end
  5 Comments
Aron
Aron on 11 Apr 2023
I make an array of the class of Herd and use the ".eat" on this array with input argument the "grasslands" array on which these "Herds" would interact (this is an agent-based modelling assignment). But I get the error already when I assign self.currentGridCell to another variable before using it as an index for the grasslands so the "self." should be the problem. I call the function in the main file as such:
clc
clearvars
close
tic
%clearing console and variables
numberOfLocations = 3;
gridDimensions = 20;
weeksSeason = 13;
timeSim = 1; %temporal resolution is a week and this for a 100
% years with each year consisting of 52 weeks, type the amount of years
% here
grasslands = zeros(gridDimensions, gridDimensions);
grasslands = grasslands + 10000;
matGrMod = [1, 1.5, 1, 0.5]; %matrix with grass modifiers for one year
matGrMod = repmat(matGrMod, [1, timeSim]); %matrix with grass modifiers for a timeSim years
names = ["Jacob","Michael","Joshua","Matthew","Andrew","Christopher","Daniel","Joseph", ...
"Ethan","Nicholas","William","Anthony","David","Ryan","Tyler"];
locations = startingLocations(numberOfLocations, gridDimensions);
herdArray = Herd.empty(3,0);
familyArray = Family.empty(3,0);
for i = 1:numberOfLocations
herdArray(i) = Herd(names(i), 200, [locations(i,1), locations(i,2)]);
familyArray(i) = Family(names(i), 200, [locations(i,1), locations(i,2)]);
end
for i = matGrMod % a for loop going through 13 weeks for each grass modifier
for j = 1:weeksSeason
grasslands = grasslands + (100*i);
grasslands(grasslands>40000) = 40000;
grasslands(grasslands<0) = 0;
herdArray.eat(grasslands) %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end
end
toc
Aron
Aron on 11 Apr 2023
The startingLocations file looks like this and the Family used so should not be a problem.
function [locations] = startingLocations(numberOfLocations, gridDimensions)
if numberOfLocations > gridDimensions*gridDimensions
throw(MException('startingLocations:tooManySamples', 'Trying to sample %d different positions from %d locations', numberOfLocations, gridDimensions*gridDimensions))
end
rng(804694) % replace 'shuffle' with your r-number here
[X,Y] = meshgrid(1:gridDimensions, 1:gridDimensions);
allLocations = horzcat(reshape(X,[],1), reshape(Y,[],1));
locationIdxs = randsample(size(allLocations, 1), numberOfLocations);
locations = allLocations(locationIdxs,:);
end

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Apr 2023
When you use an object method using dot syntax on an object, then the object is always passed as the first parameter to the method. Your code is expecting it to be passed as the second parameter to the method.

More Answers (0)

Categories

Find more on Performance and Memory in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!