Linking variable name/symbol to sequential retrieval of FRED data files.
5 views (last 30 days)
Show older comments
As a relatively new user of Matlab I would greatly appreciate suggestions on how to: 1) Assign a unique variable name to data (values) retrieved for lists of multiple items from the FRED data base and 2) Save each item as a separate .mat file. FRED delivers the data for each item as a table without its identifier. I am currently retrieving the information using the following loop:
url = 'https://fred.stlouisfed.org/';
c=fred(url);
c.DataReturnFormat='table';
c.DatetimeType='datetime';
batch = {"WPU1141","WPU1144","WPU1174","WPU3021","WPS302201","WPS511104",...
"WPU512101","WPU067904","WPU106","WPU1423"};
for ind = 1:length(batch)
batchud = batch{ind}
%startdate='01-01-2020';
%enddate='01-01-2022';
d=fetch(c,batchud);
data=d.Data{1};
TT=table2timetable(data)
recent=tail(TT,12)
end
close(c)
The output confirms that the loop is correctly reading the batch list and converting each entry to a timetable. Since each one is in turn labeled "TT", the variable name needs to be changed in order to save separately in the loop. I have tried a number of possibilities but obviously have not located the correct function, syntax, or punctuation. Any thoughts would be most welcome. Virtually all of the raw data I work with will be timetables.
Chris
15 Comments
dpb
on 18 Feb 2021
OK, you know your needs best, of course.
Just didn't want my previous comment to sound more complicated than it really is and thereby discourage you from getting something sooner rather than later in thinking would be difficult to match series.
Accepted Answer
dpb
on 16 Feb 2021
Edited: dpb
on 16 Feb 2021
"-the answer to your q is yes, this particular series are all on monthly freq and return the same dates."
Ah! Was hoping for that to be the case -- then do something more like
url = 'https://fred.stlouisfed.org/';
c=fred(url);
c.DataReturnFormat='table';
c.DatetimeType='datetime';
batch = {"WPU1141","WPU1144","WPU1174","WPU3021","WPS302201","WPS511104",...
"WPU512101","WPU067904","WPU106","WPU1423"};
d=fetch(c,batch{1});
TT=table2timetable(d.Data{1});
TT.Properties.VariableNames=batch{ind};
for ind = 2:length(batch)
d=fetch(c,batch{ind});
TT.(batch{ind})=d.Data{1};
end
close(c)
save(TT,'GLOBALBATCHDATA')
The above will return one timetable with the variables catenated by column and by name...I'd think much more convenient than a whole bunch of files.
NB: You can use dynamic variable names with the table dot notation by surrounding the variable name char() string in (). Look at the table provided in the link for the table class at the link about accessing data from a table for a full expose of all the addressing modes available and syntax for each. It can get somewhat convoluted, but there's a way to do almost anything needed.
CAUTION: Air code, check syntax carefully... :)
4 Comments
dpb
on 16 Feb 2021
Yeah, it's the detail of that content of d that need to see to understand how to dereference it.
the LHS TT.(batch{ind}) is creating a new column variable in the TT table with the name of the current batch ID. The RHS should be the data array to go there, however it is referenced.
While stopped in the debugger at that line, you should be able to write at the command line
TT.(batch{ind})=nan(height(TTT,1));
and then
head(TT)
should show the new variable in the table with value of NaN. What need is to replace the array of NaN with whatever dereference of the returned data is. I can't see that from here, unfortunately...need you to do the probing for me. :)
That's what would be helpful in the output of the whos commands I gave before...shows me the precise storage and types as opposed to a verbal description.
We can get this ironed out, it's just a little syntax/data storage issue.
More Answers (0)
See Also
Categories
Find more on Time Series Events in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!