Add columns to the tables that stored in a cell

6 views (last 30 days)
Hey all, I have a 40 x 32 cell named Cnew. On the other hand, I have a 1 x 32 table named lat and 40 x 1 table named lon. I want to add corresponding latitude (lat) and longitude (lon) columns to each table in front of the existing column. So the problem is founding corresponding latitude and longitude based on the place of each table, and add latitude and longitude column for them. I know it's maybe complicated so I use this little example to tell what I need:
As I attached Cnew, at this time Cnew looks like this:
1.JPG
But in essence, they are placed based on latitude and longitude so in fact:
2.JPG
Now I have lon and lat for this data in lon and lat table that I attached. for example:
3.JPG
Now I have just one column in every T that represents a value. I want to add 2 columns in each T, one for latitude and one for longitude. For example, Orange T has lon=44.25 and lat=24.25. Blue T has lon=44.25 and lat=24.75. Green T has lon=44.25 and lat=22.25. Red T has lon=44.75 and lat=24.25. Now I want to add related lat and lon as columns Inside each T.
I don't know how to do this.
Thank You so much.

Accepted Answer

Adam Danz
Adam Danz on 10 Feb 2020
Edited: Adam Danz on 11 Feb 2020
"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') %loads variable C2, a 40x32 cell of 336x1 tables
load('lat.mat') %loads variable lat, a 1x32 table
load('lon.mat') %loads variable lon, a 40x1 table
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'}; % do you want to change the var names?
end
end
The result, C2LatLon is a 40x32 cell array. Each element is a 336x3 table. An example is shown below.
head(C2LatLon{3}) %show first few rows of table #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') %loads variable C2, a 40x32 cell of 336x1 tables
load('lat.mat') %loads variable lat, a 1x32 table
load('lon.mat') %loads variable lon, a 40x1 table
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') %loads variable C2, a 40x32 cell of 336x1 tables
load('lat.mat')%loads variable lat, a 1x32 table
load('lon.mat')%loads variable lon, a 40x1 table
[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
  3 Comments
Stephen23
Stephen23 on 11 Feb 2020
Edited: Stephen23 on 11 Feb 2020
"organize the tables into a supertable would be more organized and would require less memory"
It would also be much simpler to work with. Splitting data up into lots of arrays usually makes it harder to work with (as you are finding out now). Just add a longitude and latitude variable to each table, then merge them all into one table (or even better, don't even split them up in the first place!), then you can process your data using the standard tools applied to that one table, e.g.:
What you are doing now totally defeats the point of the table class.
Star Strider
Star Strider on 11 Feb 2020
@Stephen — That was the essence of my (deleted since it was not Accepted) Answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!