Transfer variables within function with different properties.

1 次查看(过去 30 天)
Hi,
I wrote the following class code:
classdef Copy_of_getPortRawData
properties
PortMembers
PortWeights
end
methods
function obj=getPortRawData(c,PortID,Dates)
obj=getPortMembers(obj,c,PortID,Dates);
obj=getPortWeights(obj,Dates);
end
% 1)
function obj=getPortMembers(obj,c,PortID,Dates)
% Define input parameters
portField={'PORTFOLIO_MWEIGHT'};
override={'REFERENCE_DATE'};
nDates=size(Dates,1);
rawPortData=cell(nDates,1);
% start downloading raw portfolio data
for r=1:nDates
rawPortData{r}=portfolio(c,PortID,portField,override,datestr(Dates(r,1),'yyyymmdd'));
% simplify data storage structure and delete first row
rawPortData{r}=rawPortData{r,1}.PORTFOLIO_MWEIGHT{1,1};
% find unique portfolio members
if r==1
obj.PortMembers=rawPortData{r}(:,1);
else
obj.PortMembers=union(obj.PortMembers,rawPortData{r}(:,1),'sorted');
end
end
end
% 2)
function obj=getPortWeights(obj,Dates)
% construct matrix of weights for each portfolio member and each date
nPortMembers=size(obj.PortMembers,1);
obj.PortWeights=zeros(nDates,nPortMembers);
nDates=size(Dates,1);
for r=1:nDates
ind=ismember(obj.PortMembers,rawPortData{r}(:,1));
obj.PortWeights(r,ind)=transpose(cell2mat(rawPortData{r}(:,2)));
end
end
end
end
My question refers to the code after the line with text % 2)
How does the code look like if I want to work with the variable "rawPortData" coming from the function under the line % 1)?
thanks a lot in advance for your reply.

采纳的回答

Adam
Adam 2019-9-3
Either return it as an output argument from the previous function and pass it in as an input argument or store it as a class property.
[obj, rawPortData] =getPortMembers(obj,c,PortID,Dates)
obj=getPortWeights(obj,Dates, rawPortData);
or
obj.rawPortData{r} = ...
and add it to the properties list, though put it in a private access block, not a general access block, if you want to be neat.
  1 个评论
BdS
BdS 2019-9-3
Hi Adam
Thank you for your answer. The first version is from my point of view better as rawPortData gets big as the Dates gets big.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Portfolio 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