How I can extract an object entry from an index of cell array?

10 views (last 30 days)
Hello.
I want to extract an object which is saved in specific index of cell array.
I need to extract it because I cannot access object properties via 'dot' and cell array index.
I have a object which has an property named "dist"
I saved the object into an cell array and I later on I need to change the object proeprty and i cannot access the property via cell array indexing.
is there any way to extract the object from cell array to change the property?
function calculateDistance(obj)
tempCell = cell(1,100);
counter = 1;
tempCell{1,counter} = MyObject
tempCell(1,1).dist = 100;
end
end
end

Accepted Answer

Ameer Hamza
Ameer Hamza on 29 Nov 2020
Use the curly brackets to access the content inside the cell arrays
tempCell{1,1}.dist = 100;
  5 Comments
Ameer Hamza
Ameer Hamza on 29 Nov 2020
Following code works fine.
First i defined the class like this
classdef VehicleClass < handle
properties
dist = Inf;
end
methods
function obj = VehicleClass(dist)
obj.dist = dist(1);
end
end
end
and then run the following code
C = cell(1,10);
for i = 1:10
myObj = VehicleClass(10);
C{i,1} = myObj;
C{i,1}.dist = rand();
end
You can compare it with your code to see if there is any difference in your method.

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!