Linking variable name/symbol to sequential retrieval of FRED data files.
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
These all a monthly series over same time frame, by any chance?
If so, I'd probably combine the data into one table as columns using the batch name as the variable name.
If they're not commensurate in time, couple of choices, but simplest would be to just save each as a file with the batch name as the root filename.
I manually downloaded one; don't have the Datafeed TB and wasn't aware of the data source, either.
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)
d=fetch(c,batch{ind});
TT=table2timetable(d.Data{1});
TT.Properties.VariableNames=batch{ind};
save(TT,batch{ind})
end
close(c)
will put the batch in as variable name and write each in turn to a .mat file by that name as well.
We could be more sophisticated if knew answer to first Q?
cris
on 16 Feb 2021
cris
on 16 Feb 2021
dpb
on 16 Feb 2021
Hmmm....set a breakpoint there on that line
TT.(batch{ind})=d.Data{1};
and show what
whos TT d
returns as well as
head(TT)
head(d)
(I'm assuming d is a table???)
Too bad don't have the toolbox here; remote debugging is much tougher when can't replicate the system.
Stop in debugger at the line and
save 'state'
will save the entire context of the function at that point to a .mat file "state.mat"
Attach that file with the paperclip icon...then I can poke directly at what d is to figure out the dereferencing issues...it's not clear to me how the table inside a table is getting generated by the above...in particular since it is in a loop what is different the second pass from the first.
Attach your actual m-file as well so have an accurate copy of it as well.
cris
on 17 Feb 2021
cris
on 17 Feb 2021
dpb
on 17 Feb 2021
OK, I see. Having the actual returned table d helps. What I'm still uncertain about is why the first worked as intended since (I think?) the code was in a loop so the same dereferencing should have happened both passes.
Anyways, try the following for the assignment -- BTW, the returned d.SeriesID series name string contains a leading blank that will be significant in the column names if used as is; since you've got to have the list of desired series anyway, I think I'd just continue to use it as the name generator.
The following should then work--and I see from the above code why the first is different -- thought you were using a version I posted later that has the first case already handled:
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}); % get the first series only
TT=table2timetable(d.Data{1}); % create the timetable
TT.Properties.VariableNames=batch{1}; % set first variable name
for ind = 2:length(batch) % now get the rest of the series, append to TT
d=fetch(c,batch{ind}); % get the first series only
TT.(batch{ind})=d.Data{:}.Var2; % dereference the cell containing the table; get the Var2 data
end
close(c)
save(TT,'WHATEVERGLOBALNAMEWANT')
I should have recognized what d was sooner, but sometimes hard to wrap head around... :)
d.Data{:}
returns the table; you can reference the return value of it same as if saved as a variable so the dot addressing to the internal variable name works. It is returned as a default table with VarN as the variables names so just hardcode 'Var2'
cris
on 18 Feb 2021
dpb
on 18 Feb 2021
""to assign or create a variable...the # of rows must match the height of the table." Turns out item #4 is shorter than the first four. "
Well, in that case, the fundamental assumption that each of these series is commensurate in the time vector is not upheld and so incorporating them together in one timetable isn't appropriate after all. That was why asked the Q? earlier.
In this case you'll need to either
1. Create the initial time series that is the maximum number that is in the whole series and then place the data from each in turn at the correct location in the series with a missing value indicator for those not present; or
2. Revert to the earlier path shown of saving each as its own timetable and either writing each as a separate file or create a cell array of tables.
cris
on 18 Feb 2021
dpb
on 18 Feb 2021
OK. However, I will note that I think you could get that aggregation while doing this by only one or two more lines of code. If you were to go ahead and create a timetable for each as were on download, then retime it to the full time interval in the one big TT, you'd have both worlds...the data series all in one convenient file to be obtained at will plus the commensurate time lengths for all.
Just a thought -- the future may not be so far away or such a stretch as it seems... :)
cris
on 18 Feb 2021
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
More Answers (0)
Categories
Find more on Data Preprocessing 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!