How I can extract an object entry from an index of cell array?
10 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Ameer Hamza
2020-11-29
Use the curly brackets to access the content inside the cell arrays
tempCell{1,1}.dist = 100;
5 个评论
Ameer Hamza
2020-11-29
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!