1
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.

9
  • I hope for a solution without eval(). Commented Feb 20, 2017 at 16:41
  • 2
    Dynamic variables are a terrible, terrible, terrible idea. I am glad you mention not using eval because that is also a terrible terrible idea. Please tell me you are not using globals! Commented Feb 20, 2017 at 16:41
  • :D No I am not using globals Commented Feb 20, 2017 at 16:42
  • You don't want dynamic variable names. Dynamic field references in a structure (your working code) is fine, just use that. Why do you want to use dynamic variables anyway? Commented Feb 20, 2017 at 16:48
  • 3
    How did you obtain the right hand side of the last code bit? You shouldn't have created that in the first place, but should've saved directly as the left hand side structure. Commented Feb 20, 2017 at 17:01

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.