I have a table
column1data = [11; 22; 33];
column2data = [44; 55; 66];
column3data = [77; 88; 99];
rows = {'name1', 'name2', 'name3'};
T = table(column1data, column2data, column3data);
T.Properties.RowNames = rows
column1data column2data column3data
name1 11 44 77
name2 22 55 88
name3 33 66 99
and a struct array
S(1).rownamefield = 'name3';
S(2).rownamefield = 'name1';
S(3).rownamefield = 'name2';
S(1).columnnumberfield = 1;
S(2).columnnumberfield = 3;
S(3).columnnumberfield = 2;
S(1).field3 = [];
S(2).field3 = [];
S(3).field3 = [];
rownamefield columnnumberfield field3
1 'name3' 1 []
2 'name1' 3 []
3 'name2' 2 []
The struct array S
contains criteria needed to pick the variable from table T
. Once the variable is picked, it has to be copied from table T
to an empty field in struct S
.
S(1).rownamefield
contains the name of the row in table T
where the target variable resides. S(1).columnnumberfield
contains the number of the column in table T
with the target variable. So S(1).rownamefield
plus S(1).columnnumberfield
are practically the coordinates of the target variable in table T
. I need to copy the target variable from table T
to field3 in the struct array: S(1).field3
. This has to be done for all structs so it might need to be in a for loop, but I am not sure.
The output should look like this:
rownamefield columnnumberfield field3
1 'name3' 1 33
2 'name1' 3 77
3 'name2' 2 55
I have no idea how to approach this task. This is, of course, a simplified version of the problem. My real data table is 200x200 and the struct array has over 2000 structs. I will greatly appreciate any help with this.