MATLAB Answers

How to index through variable list in a table after doing calculation with each variable

18 views (last 30 days)
Georgia Kennedy
Georgia Kennedy on 12 Feb 2021
Commented: Stephen Cobeldick on 13 Feb 2021
Hello,
Fairly new to matlab, and I am having trouble with the table functionality. I am reading two .csv files into a table and I want to cycle through a list of selected variables, compare the data corresponding to that variable in each table, output a plot, then move on to the next.
My issue is that I can't find a good way to index through each variable without brute force typing out every vame.
b_table = readtable('base.csv');
b_table(2,:) = [];%remove 'Source Address' row
f_table = readtable('test.csv');
f_table(2,:) = [];%remove 'Source Address' row
%Baseline data
x_base = (b_table.A); %where A is a variable in the table
x_baseunits = x_base(1);
x_base(1) = [];
base_array =cellfun(@str2num, x_base);%convert cell array to num array
Ideally I just want to do a for or while loop in a function to spit out all the info for variables but the main issue is how to cycle through the variables.
I know this is incorrect, but something like
myVars = { A , B, C, D}; % list of variables I want to extract data from
b_table.A(:, myVars);
Any help would be greatly appreciated!!

Answers (1)

Jeff Miller
Jeff Miller on 13 Feb 2021
myVars = {'Var1name' 'Var2name' 'Var3name'}; % list the names of the variables you want
for iVar=1:numel(myVars)
thisColumn = b_table.(myVars{iVar});
% do what you want with the data in this column.
end
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!