Reputation: 3048
HF.EH = EH.Return_EH;
I want to do the above statement. HF is a table, so is EH.
str = 'HE'
HF.(str) = (str).Return_EH;
The last line of code is not working. "HF.(str)" is fine. "(str).Return_EH" is not. How can I make "(str).Return_EH" work?
Right now my code looks like that:
EH = importraw('HFRX_Equity_Hedge_Index.csv', 'EH');
EMN = importraw('HFRX_Equity_Market_Neutral_Index.csv', 'EMN');
EDI = importraw('HFRX_Event_Driven_Index.csv', 'EDI');
FICA = importraw('HFRX_FI-Convertible_Arbitrage_Index.csv', 'FICA');
MCTA = importraw('HFRX_Macro_CTA_Index.csv', 'MCTA');
MAI= importraw('HFRX_Merger_Arbitrage_Index.csv', 'MAI');
RVA = importraw('HFRX_Relative_Value_Arbitrage_Index.csv', 'RVA');
% sanity check
if not(isequal(EH.Date, EMN.Date, EDI.Date, FICA.Date, MCTA.Date, MAI.Date, RVA.Date));
error('Mismatch in Data');
end
% merge Hedgefund Data
HF = array2table(zeros(size(EH,1),8), 'VariableNames',{'Date', ...
'EH', 'EMN', 'EDI', 'FICA', 'MCTA', 'MAI', 'RVA'});
HF.Date = EH.Date;
HF.EH = EH.Return_EH;
HF.EMN = EMN.Return_EMN;
HF.EDI = EDI.Return_EDI;
HF.FICA = FICA.Return_FICA;
HF.MCTA = MCTA.Return_MCTA;
HF.MAI = MAI.Return_MAI;
HF.RVA = RVA.Return_RVA;
I thought there should be a better way.
Upvotes: 1
Views: 309
Reputation: 10440
Instead of looking for dynamic variable names, you can use a structure in the same way you use the table HF
. First, you would import all data to one structure S
:
S.EH = importraw('HFRX_Equity_Hedge_Index.csv', 'EH');
S.EMN = importraw('HFRX_Equity_Market_Neutral_Index.csv', 'EMN');
% and so on...
Then in your sanity check, and everywhere else in your code, you need to add S.
before this tables. Now you can replace the last part with:
fld = fieldnames(S);
for k = 1:numel(fld)
HF.(fld{k}) = S.(fld{k}).(['Return_' fld{k}]);
end
Upvotes: 1