How to save portfolio objects to an array?

1 回表示 (過去 30 日間)
Nirmal Hirani
Nirmal Hirani 2022 年 10 月 7 日
回答済み: Sailesh Kalyanapu 2022 年 10 月 10 日
p = Portfolio;
c = Portfolio;
d = [p c]
d =
1×2 Portfolio array with properties: BuyCost SellCost RiskFreeRate AssetMean AssetCovar TrackingError TrackingPort Turnover BuyTurnover SellTurnover Name NumAssets AssetList InitPort AInequality bInequality AEquality bEquality LowerBound UpperBound LowerBudget UpperBudget GroupMatrix LowerGroup UpperGroup GroupA GroupB LowerRatio UpperRatio MinNumAssets MaxNumAssets BoundType
How do i store objects in an array and then extract any specific object from that array? I want to do this.
d.p.BuyCost
Thanks.
  1 件のコメント
dpb
dpb 2022 年 10 月 8 日
Indexing; don't compound-nest similar structs; that will become totally unworkable.
d(2).BuyCost
You can create an associated name array to use as id if want a way to find specific element of the array or add that ID as a field.

サインインしてコメントする。

採用された回答

Sailesh Kalyanapu
Sailesh Kalyanapu 2022 年 10 月 10 日
As per my understanding, you are looking to store objects of type ‘Portfolio’ in a way to later access them using dot indexing.
You can do so by saving the objects in a struct.
>> d = struct
>> obj1 = Portfolio;
>> obj2 = Portfolio;
>> d.p = obj1;
>> d.c = obj2;
% To access required PropertyName
>> d.p.PropertyName (or) d.c.PropertName
For more information related to ‘struct’, please refer to the following MATLAB documentation:
In case you want to store the objects in an array, you could then use numeric(array) indexing to first select the object of choice and then use dot indexing to access the required Property.
>> p = Portfolio;
>> c = Portfolio;
>> d = [p c];
% To access required PropertyName of ith object in array.
>> d(i).PropertyName

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePortfolio Optimization and Asset Allocation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by