MATLAB: Splitting a Table at Every N Rows

splittingtable

I have a single table with 58 years worth of tax data for 75 different countries. It is currently organized with the countries stacked on top of each other alphabetically. Hence, I have 58 years of data for Argentina, followed by 58 years of data for Austria, and so on. I would like to split it at every 58th row, so that I can store each country's data in its own separate table. Is there a quick way I can do this?

Best Answer

You can use this script to split your table as mentioned in the question. Replace myTable with the name of your table. It will create a cell array seperatedCountries.
nunberCountries = 75;
noumberYears = 58;
rowOndexVector = repelem(1:nunberCountries, noumberYears);
seperatedCountries = splitapply(@(x) {x}, cell2mat(myTable{:,:}), findgroups(rowOndexVector));
seperatedCountries{1} will contain data about first country (i.e. Argentina). Similarly seperatedCountries{1} will contain data about second country (i.e. Austria) as so on.