필터 지우기
필터 지우기

Transfer variables within function with different properties.

조회 수: 2 (최근 30일)
BdS
BdS 2019년 9월 3일
댓글: BdS 2019년 9월 3일
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