# How to avoid creating lots of variables from calculation from a table

1 view (last 30 days)
Wah On Ho on 23 May 2023
Commented: Wah On Ho on 24 May 2023
Hi All,
I am still a beginner at this...
I have a table of data (dataTable) that the code below sets up, the point being there is a variable name ('Var') and a suffix of 1 to 10 in this example. From 'dataTable' I want to calculate the mean of a defined section for each variable, and ends with compiling the results into one array.
Is there a way to do this without having to create all the separate variables meanVar1...meanVar10 as below from the dataTable? Thanks and much appreciated.
Time = [0;1;2;3;4;5;6;7;8;9;10];
Var1 = randi([-10 10],1,11)';
Var2 = randi([-10 10],1,11)';
Var3 = randi([-10 10],1,11)';
Var4 = randi([-10 10],1,11)';
Var5 = randi([-10 10],1,11)';
Var6 = randi([-10 10],1,11)';
Var7 = randi([-10 10],1,11)';
Var8 = randi([-10 10],1,11)';
Var9 = randi([-10 10],1,11)';
Var10 = randi([-10 10],1,11)';
dataTable = table(Time, Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10);
meanVar1 = mean(dataTable.Var1(3:7));
meanVar2 = mean(dataTable.Var2(3:7));
meanVar3 = mean(dataTable.Var3(3:7));
meanVar4 = mean(dataTable.Var4(3:7));
meanVar5 = mean(dataTable.Var5(3:7));
meanVar6 = mean(dataTable.Var6(3:7));
meanVar7 = mean(dataTable.Var7(3:7));
meanVar8 = mean(dataTable.Var8(3:7));
meanVar9 = mean(dataTable.Var9(3:7));
meanVar10 = mean(dataTable.Var10(3:7));
meanscompilation = [meanVar1;meanVar2;meanVar3;meanVar4;meanVar5;meanVar6;meanVar7;meanVar8;meanVar9;meanVar10];

Steven Lord on 23 May 2023
If all the variables you want to add to the table array are the same type, create one array and use array2table or (given that you have time data) perhaps array2timetable.
Time = [0;1;2;3;4;5;6;7;8;9;10];
VarData = randi([-10 10], 11, 10);
names = ["Time", "Var"+(1:10)];
dataTable = array2table([Time, VarData], 'VariableNames', names)
dataTable = 11×11 table
Time Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ _____ 0 3 6 4 -8 -7 5 8 1 0 -2 1 -6 2 4 0 10 -10 10 -10 2 1 2 3 -10 2 0 -6 -6 4 -4 -3 -5 3 -8 1 -5 0 -2 3 -4 -10 -9 -1 4 -10 -10 -6 7 7 10 7 -1 4 -3 5 -6 8 -5 -8 -10 -8 -9 9 3 -7 6 3 -3 -4 -10 -7 0 -3 -3 7 -5 7 -9 8 10 4 -9 2 -2 -6 -6 10 8 3 -7 8 -7 -4 -10 -9 -10 -7 -1 9 -3 9 6 8 -4 -6 -8 2 -3 -3 10 1 -9 7 -3 -3 -9 4 7 1 7
Since your release predates the introduction of the ability to perform math on tables directly (release R2023a) I'd use varfun.
M = varfun(@mean, dataTable)
M = 1×11 table
mean_Time mean_Var1 mean_Var2 mean_Var3 mean_Var4 mean_Var5 mean_Var6 mean_Var7 mean_Var8 mean_Var9 mean_Var10 _________ _________ _________ _________ _________ _________ _________ _________ _________ _________ __________ 5 -2.6364 -0.45455 1.9091 -1.5455 -3.1818 -2.6364 -0.18182 -2.2727 -1 -0.81818
If upgrading to release R2023a or later is an option:
M23a = mean(dataTable)
M23a = 1×11 table
Time Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 ____ _______ ________ ______ _______ _______ _______ ________ _______ ____ ________ 5 -2.6364 -0.45455 1.9091 -1.5455 -3.1818 -2.6364 -0.18182 -2.2727 -1 -0.81818
Note that both these approaches by default will also operate on the Time variable in dataTable. You could specify InputVariables when calling varfun to skip operating on Time.
M = varfun(@mean, dataTable, 'InputVariables', 2:width(dataTable))
M = 1×10 table
mean_Var1 mean_Var2 mean_Var3 mean_Var4 mean_Var5 mean_Var6 mean_Var7 mean_Var8 mean_Var9 mean_Var10 _________ _________ _________ _________ _________ _________ _________ _________ _________ __________ -2.6364 -0.45455 1.9091 -1.5455 -3.1818 -2.6364 -0.18182 -2.2727 -1 -0.81818
Wah On Ho on 24 May 2023
That's great thank you very much.
I did make a slight mod to your solutions to select a section of data from the middle of the table...
M = varfun(@mean, dataTable(3:7,:))
M = varfun(@mean, dataTable(3:7,:), 'InputVariables', 2:width(dataTable))

### Categories

Find more on Data Type Conversion in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!