MATLAB: Copying variables from table to struct based on certain criteria -


i have 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 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 pick variable table t. once variable picked, has copied table t empty field in struct s.

s(1).rownamefield contains name of row in table t target variable resides. s(1).columnnumberfield contains number of column in table t target variable. s(1).rownamefield plus s(1).columnnumberfield practically coordinates of target variable in table t. need copy target variable table t field3 in struct array: s(1).field3. has done structs might need in loop, not sure.

the output should this:

    rownamefield columnnumberfield field3 1     'name3'           1            33 2     'name1'           3            77 3     'name2'           2            55 

i have no idea how approach task. is, of course, simplified version of problem. real data table 200x200 , struct array has on 2000 structs. appreciate this.

% extract table data , linearly index tdata   = t{:,:}; [~,row] = ismember({s.rownamefield}, t.properties.rownames); col     = [s.columnnumberfield]; pos     = sub2ind(size(tdata),rowpos, col); val     = tdata(pos);  % assign struct ii = 1:numel(s)     s(ii).field3 =  val(ii); end 

instead of for-loop, can use suever's solution deal() assign values in 1 go (have num2cell(val) first). whatever faster , more intuitive.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -