Append the tabelename to a variable name in a table

1 Ansicht (letzte 30 Tage)
alpedhuez
alpedhuez am 14 Jun. 2018
Kommentiert: alpedhuez am 7 Jul. 2018
I have a table T. There are variables a,b,... in T. I would like to rename variables so that their names will be T_a,T_b,...etc. Please advise.
  15 Kommentare
Stephen23
Stephen23 am 14 Jun. 2018
Bearbeitet: Stephen23 am 14 Jun. 2018
@alpedhuez: here is some pseudocode. Adjust to suit your situation:
S = dir(...)
T = readtable(S(1).name)
T.City = ... cityname: from filename ???
for k = 2:numel(S)
U = readtable(S(k).name)
U.City = ... cityname: from filename ???
T = [T;U]
end
This is untested, it is just to illustrate one way to import multiple tables so that you do not need to magically access the table names, and can simply include the city name as a column in the table.
alpedhuez
alpedhuez am 14 Jun. 2018
Let me work on it.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 14 Jun. 2018
Bearbeitet: Steven Lord am 14 Jun. 2018
Have a variable in the combined table named City. Rows in the combined table that came from your New York table would have NY in that variable, and those that came from your San Francisco table would have SF in that variable. Once you've added that variable to each of your tables, join them together using City as your key variable.
Once you have that as a variable in your table you could use it as the grouping variable in calls to groupsummary if you want to compute, say, the total population for all the regions / boroughs / etc. in your table that are in NY.
mytable = table({'NY'; 'SF'; 'NY'; 'NY'; 'SF'}, [1; 2; 3; 4; 5], ...
'VariableNames', {'City', 'pop'})
totalPopulation = groupsummary(mytable, 'City', 'sum', 'pop')

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by