"I want to add 2 columns in each T, one for latitude and one for longitude."
C2 is a 40x32 cell array of 336x1 tables. Is your goal to create a 40x32 cell array of 336x3 tables where the two additional columns are latitude and longitude values?
If that's what you're doing,
load('C2.mat')
load('lat.mat')
load('lon.mat')
C2LatLon = cell(size(C2));
for i = 1:size(C2,2)
for j = 1:size(C2,1)
n = height(C2{j,i});
C2LatLon{j,i} = [repmat(lon(j,1),n,1), repmat(lat(1,i),n,1), C2{j,i}];
C2LatLon{j,i}.Properties.VariableNames = {'lon','lat','data'};
end
end
The result, C2LatLon is a 40x32 cell array. Each element is a 336x3 table. An example is shown below.
head(C2LatLon{3})
ans =
8×3 table
lon lat data
_____ _____ _____
45.25 24.25 1.08
45.25 24.25 8.88
45.25 24.25 25.75
45.25 24.25 35.46
45.25 24.25 0.02
45.25 24.25 0
45.25 24.25 0.16
45.25 24.25 0.11
Super table format
However, organizing the tables into a supertable would be more organized and would require less memory.
Here's a variant of Star Strider's deleted answer.
load('C2.mat')
load('lat.mat')
load('lon.mat')
T = array2table(C2, 'VariableNames', lat.Properties.VariableNames);
T = [lat; T];
lon = [table(nan,'VariableNames',{'lon'}); lon];
T = [lon, T];
Here's a sample of what that looks like
T(1:8,1:6)
ans =
8×6 table
lon lat1 lat2 lat3 lat4 lat5
_____ _____________ _____________ _____________ _____________ _____________
NaN {[ 24.25]} {[ 24.75]} {[ 25.25]} {[ 25.75]} {[ 26.25]}
44.25 {336×1 table} {336×1 table} {336×1 table} {336×1 table} {336×1 table}
44.75 {336×1 table} {336×1 table} {336×1 table} {336×1 table} {336×1 table}
45.25 {336×1 table} {336×1 table} {336×1 table} {336×1 table} {336×1 table}
45.75 {336×1 table} {336×1 table} {336×1 table} {336×1 table} {336×1 table}
46.25 {336×1 table} {336×1 table} {336×1 table} {336×1 table} {336×1 table}
46.75 {336×1 table} {336×1 table} {336×1 table} {336×1 table} {336×1 table}
47.25 {336×1 table} {336×1 table} {336×1 table} {336×1 table} {336×1 table}
Super Table 2 (Recommended)
This arrangement would be easiest to use. Replace row and column names with something more meaningful.
load('C2.mat')
load('lat.mat')
load('lon.mat')
[latMat, lonMat] = ndgrid(lat{:,:}, lon{:,:});
C = cellfun(@(T){T{:,:}}, C2);
LLT = array2table([latMat(:).'; lonMat(:).'], 'RowNames', {'Lat','Lon'});
T = [LLT; array2table(cell2mat(C(:)'),'RowNames',compose('Data%d',1:height(C2{1})))];
Here's a sample of the first 8 rows and 10 columns.
T(1:8,1:10)
ans =
8×10 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10
_____ _____ _____ _____ _____ _____ _____ _____ _____ _____
Lat 24.25 24.75 25.25 25.75 26.25 26.75 27.25 27.75 28.25 28.75
Lon 44.25 44.25 44.25 44.25 44.25 44.25 44.25 44.25 44.25 44.25
Data1 1.08 1.06 1.08 0.97 0.96 1.05 1 0.87 0.45 0
Data2 39.79 20.96 8.88 3.83 3.03 2.55 0.42 0.67 1.46 2.73
Data3 20.2 23.32 25.75 27.42 30.01 32.44 30.04 28.46 28.27 28.78
Data4 31.9 34.19 35.46 35.01 32.79 30.68 32.92 31.81 23.53 10.19
Data5 0 0 0.02 0.19 0.38 0.43 0.43 0.49 0.15 0
Data6 0 0 0 0 0 0 0 0.01 0.04 0.08
Best Answer