Looping over a set of names and creating a set of variables

8 views (last 30 days)
I currently have download historical quotes for GE, Ford and 3M in both daily and monthly frequencies. I can do all of this with fetch(.), but this means I need to loop across four different arguments: the names ('GE', 'F', 'MMM'), the starting and ending dates, and the frequencies ('d', 'm'). I would then store them into financial time series objects using 'fints.'
(1.) Retrieve the data; (2.) Store it in a time series object within a structure 'data' (say, data.ge, data.f, data.mmm).
Currently, I know how to do exactly that for one series:
ge = fetch(yahoo, 'ge', 'Adj Close', '1/2/1962','1/17/2016','d');
data.ge = fints(ge(:,1), ge(:,2), '', 'd');
I also have the correct names for Ford and 3M Company on yahoo!finance, as well as the appropriate starting and ending dates for each series. If I was doing this work on Stata, I know exactly what I would do because I could use foreach to loop over list of names exactly like I would loop over values -- but, as a newbie on matlab, I need some help! The odds are, I might not even approach the problem the right way for this program.
Thanks
  3 Comments
Econstudent
Econstudent on 21 Jan 2017
I solved my problem using cell arrays. I basically imported series in 'fints' formats and placed the 'fints' objects in an array (and I use a structure to define my parameters (names, frequencies, etc.)):
for i = 1:size(fin.names, 2)
for j = 1:size(fin.freq, 2)
% Importing series from Yahoo!
data_fin{i,j} = fetch(yahoo, fin.names{i}, fin.type, fin.first{i},...
fin.last, fin.freq{j});
% Transform objects into 'FINancial Times Series'
data_fin{i,j} = fints(data_fin{i,j}(:,1), data_fin{i,j}(:,2), '', ...
fin.freq{j});
% Changing series names:
data_fin{i,j} = chfield(data_fin{i,j}, 'series1', ...
[fin.names{i}, '_', fin.freq{j}]);
end
end
If Stata users ever come across this problem in Matlab, my original intention was to write a code of the sort:
foreach i in ge mmm f {
foreach j in d m {
mat series_`i'`j' = some commande to import data
}
}
But instead of filling up the workspace with a bunch of variables named using indexes, I created a matrix-like object and positionned stuff into it using actual indexes.
EPAULW
EPAULW on 12 May 2020
Thanks. A good solution given the way Matlab is used.
The original approach I had in mind is still the one I will use in Stata, because it names individual columns in a useful way.

Sign in to comment.

Answers (0)

Categories

Find more on Strategy & Logic in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!