Returning properties of a vector of objects as an vector
4 次查看(过去 30 天)
显示 更早的评论
Is it possible to return the properties of objects in a vector as a vector or array?
Given the phone book example on https://de.mathworks.com/help/matlab/matlab_oop/finding-objects-having-specific-settings.html:
classdef PhoneBook < dynamicprops
properties
Name
Address
Number
end
methods
function obj = PhoneBook(n,a,p)
obj.Name = n;
obj.Address = a;
obj.Number = p;
end
end
end
We add some entries:
PB(1) = PhoneBook('Nancy Vidal','123 Washington Street','5081234567');
PB(2) = PhoneBook('Nancy Vidal','123 Main Street','5081234568');
PB(3) = PhoneBook('Nancy Wong','123 South Street','5081234569');
The following call now returns a list of ans, which is hard to work with:
PB.Number % returns a list of ans, but a vector would be prefered
0 个评论
采纳的回答
Ameer Hamza
2020-3-18
编辑:Ameer Hamza
2020-3-18
Try
v = {PB.Number};
You can also use
v = [PB.Number];
but I am not sure whether this will be helpful.
更多回答(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!