MATLAB Answers

BdS
0

Transfer variables within function with different properties.

Asked by BdS
on 3 Sep 2019
Latest activity Commented on by BdS
on 3 Sep 2019
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.

  0 Comments

Sign in to comment.

1 Answer

Answer by Adam
on 3 Sep 2019
 Accepted Answer

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 Comment

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.

Sign in to comment.