How can I create a time series of data where each month's data is stored in a structure?

2 views (last 30 days)
So I have a structure of 197 components (each representing one month of regression data. The fields I wish to export into excel are titled mv (which itself is a strcuture where each component has regression data for a different country) and the country and industry rep cells which are logicals that say which countries/industries are represented. I need to create a time series of the data stored in the structure, and I figured importing it into excel first might be easier. Does anyone have any thoughts about the smartest way to do this? Here is the code for the structures:
regression_structure(i).rep_industries = industry_rep_cell; regression_structure(i).rep_countries = country_rep_array; regression_structure(i).all_industries = u_industries; regression_structure(i).all_countries = u_country_codes; regression_structure(i).beta_multi = {mv};
This code is in a for loop which will loop 197 times for each month of data we are using.

Answers (1)

Aditya
Aditya on 20 Jan 2025
Hi Andrew,
To create a time series from a structure where each component represents monthly regression data, follow these steps to organize and export the data into Excel:Steps to Create a Time Series
1. Initialize Data Storage:
  • Create an empty cell array or table to store the extracted data for each month and country.
2. Loop Through the Structure:
  • Iterate over each month in your "regression_structure".
  • For each month, iterate over each country in the "beta_multi" structure.
3. Extract and Organize Data:
  • Extract relevant fields such as "rep_industries", "rep_countries", and "beta_multi" for each month and country.
  • Store this information in a row of your data storage.
4. Export to Excel:
  • Use MATLAB’s "writetable" function to export the organized data to an Excel file.
Example Code
Here is a simplified example:
% Initialize cell array to store data
data = {};
% Loop through each month
for i = 1:197
% Get current month's data
current_month = regression_structure(i);
% Loop through each country in mv structure
countries = fieldnames(current_month.beta_multi);
for j = 1:length(countries)
country = countries{j};
% Extract relevant data
rep_industries = current_month.rep_industries;
rep_countries = current_month.rep_countries;
beta_data = current_month.beta_multi.(country);
% Append data to cell array
data = [data; {i, country, rep_industries, rep_countries, beta_data}];
end
end
% Convert cell array to table
data_table = cell2table(data, 'VariableNames', {'Month', 'Country', 'RepIndustries', 'RepCountries', 'BetaData'});
% Write table to Excel
writetable(data_table, 'regression_time_series.xlsx');
I hope this helps!

Tags

Community Treasure Hunt

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

Start Hunting!