Returning properties of a vector of objects as an vector

조회 수: 4 (최근 30일)
Nicolas Bourbaki
Nicolas Bourbaki 2020년 3월 18일
댓글: Ameer Hamza 2020년 3월 19일
Is it possible to return the properties of objects in a vector as a vector or array?
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

채택된 답변

Ameer Hamza
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.
  댓글 수: 2
Nicolas Bourbaki
Nicolas Bourbaki 2020년 3월 19일
Thanks a lot for the answer.
Now that I see it, it's obvious.
This was exactly what I was looking for.
Ameer Hamza
Ameer Hamza 2020년 3월 19일
Glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by