matlab - Struggling to index into a column vector produced using the eval function -
i have several tables, each massive amount of data (20mb each). let's call them table_1, table_2, table_3, etc. structure of these tables similar, identical column headers (but varying number of rows). let's attribute_a, attribute_b, etc.
i interested in performing calculations on attribute_b each table. these calculations involve looping through attribute_b.
i start creating master list of table names.
table_list = [table_1, table_2, ... table 10];
now can iterate through , perform calculations. doing single calculations works fine using eval , concatentation of column vector name function of value of current_table:
for current_table = table_list peak_value = max(eval(strcat(current_table,"attribute_c")));
i run trouble when perform calculations require iterating through column vectors. instance, following fails.
for current_table = table_list = 1:length(eval(strcat(current_table,".attribute_b") x = x + eval(strcat(current_table,".attribute_b"))(i);
matlab gets hung when try marry evaluated column , desired index value of i. there way this?
i understand if made single structure of of data easier (not using string list combining data). want iterate through each of tables without re-writing data.
as excaza pointed out, usage of eval
in kind of problem discouraged. 1 safer , more elegant approach use structures.
here example of program should useful uses structures
clear % intialize structure hold tables tables = struct; % list of table names table_names = {'table_1' 'table_2' 'table_3'}; % number of tables ntables = length(table_names); % fill tables data % tables can nested structures more 1 data item in them in % each substructure itb=1:ntables % tables.(table_names{itb}) = rand(3); % load files names table_n.txt tables.(table_names{itb}) = load([table_names{itb},'.txt']); end % perform operations on components jtb=1:ntables % using temporary array % temp = []; % temp = tables.(table_names{jtb}); % a(jtb) = max(max(temp)); % b(jtb) = temp(2,1); % .. , such % or directly a(jtb) = max(max(tables.(table_names{jtb}))); b(jtb) = tables.(table_names{jtb})(2,1); end
you have list of names , use them create either nested structures or else - arrays here - inside of main structure called tables
. can populate data files, in case used rand
. both collecting data , operations on can done loops. in second loop have example how can manipulate data directly or indirectly using temporary arrays.
this relatively broad topic feel free ask questions , i'll add them answer.
Comments
Post a Comment