필터 지우기
필터 지우기

Problem with a Loop

조회 수: 3 (최근 30일)
Emil Petersen
Emil Petersen 2023년 4월 5일
답변: Steven Lord 2023년 4월 5일
I have this code where i try to get the different ETFs into a variable which is equal to their geographical focus. The code looks like this and the problem occurs in the last line.
% Loading data for the period 2013:01 to 2023:01
RiskFreeRate = readmatrix('RiskFree.xlsx','Range',[3 3 28 123]);
Geo = readtable('Data.xlsx','range', [1 2 3314 2]);
Active_ETFS = readtable('Data.xlsx','range',[2 14 3314 134]);
% All risk free rates
US = RiskFreeRate(1,:);
Germany = RiskFreeRate(2,:);
UK = RiskFreeRate(3,:);
Italy = RiskFreeRate(4,:);
France = RiskFreeRate(5,:);
Japan = RiskFreeRate(6,:);
Canada = RiskFreeRate(7,:);
Sweden = RiskFreeRate(8,:);
Portugal = RiskFreeRate(9,:);
Euro = RiskFreeRate(10,:);
NewZealand = RiskFreeRate(11,:);
Israel = RiskFreeRate(12,:);
Indonesia = RiskFreeRate(13,:);
India = RiskFreeRate(14,:);
HongKong = RiskFreeRate(15,:);
China = RiskFreeRate(16,:);
Taiwan = RiskFreeRate(17,:);
Switzerland = RiskFreeRate(18,:);
Africa = RiskFreeRate(19,:);
Singapore = RiskFreeRate(20,:);
Russia = RiskFreeRate(21,:);
Norway = RiskFreeRate(22,:);
Turkey = RiskFreeRate(23,:);
Vietnam = RiskFreeRate(24,:);
Brazil = RiskFreeRate(25,:);
Australia = RiskFreeRate(26,:);
%Adding the geographical focus to the ETFs
GeoETF=[Geo Active_ETFS]
% Create a structure to store the ETF data for each geographical focus
etf_data.Australia = [];
etf_data.Brazil = [];
etf_data.Canada = [];
etf_data.China = [];
etf_data.Europe = [];
etf_data.France = [];
etf_data.Germany = [];
etf_data.Global = [];
etf_data.HongKong = [];
etf_data.India = [];
etf_data.Indonesia = [];
etf_data.Israel = [];
etf_data.Italy = [];
etf_data.Japan = [];
etf_data.Korea = [];
etf_data.NewZealand = [];
etf_data.Norway = [];
etf_data.Portugal = [];
etf_data.Russia = [];
etf_data.Singapore = [];
etf_data.SouthAfrica = [];
etf_data.Sweden = [];
etf_data.Switzerland = [];
etf_data.Taiwan = [];
etf_data.Turkey = [];
etf_data.UK = [];
etf_data.USA = [];
% Loop through the ETF matrix and assign each ETF to its corresponding geographical focus
for i = 1:size(GeoETF, 1)
geo_focus = GeoETF{i, 1};
etf_returns = GeoETF{i, 2:end};
% Assign the ETF returns to the corresponding geographical focus in the structure
etf_data.(geo_focus) = etf_returns; %SAYS "Argument to dynamic structure reference must evaluate to a valid field name"
end
The first row of my dataset is 'Israel' which it give me thee error to. i have also tried different regions but it does not make any difference. It basicly says that etf_data.Israel doesent exist which it does.
  댓글 수: 1
Mathieu NOE
Mathieu NOE 2023년 4월 5일
you would increase the chances of success if you provide the excel files along the code

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2023년 4월 5일
Instead of reading the data into a matrix, creating individual variables, and then storing those variables in a struct array I'd consider reading the data into a table array with readtable and setting the names of the variables in the table to the country names. Alternately if you have to read the data as a matrix, use array2table to convert it into a table with named variables. I'll demonstrate the latter approach so I don't have to create a file.
data = magic(5)
data = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
countryNames = ["US", "Germany", "UK", "Italy", "France"];
T = array2table(data, 'VariableNames', countryNames)
T = 5×5 table
US Germany UK Italy France __ _______ __ _____ ______ 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
To access this data you can use any of the approaches described on this documentation page. Dot notation and curly braces are probably going to be the most useful for you.
dataFromItaly = T.Italy
dataFromItaly = 5×1
8 14 20 21 2
dataFromFrance = T{:, 5}
dataFromFrance = 5×1
15 16 22 3 9
dataFromFrance2 = T{:, "France"}
dataFromFrance2 = 5×1
15 16 22 3 9

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by