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

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!!

回答 (1 件)

Jeff Miller
Jeff Miller 2021 年 2 月 13 日
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 件のコメント

Georgia Kennedy
Georgia Kennedy 2021 年 2 月 13 日
Perfect! Thank you so much! If you dont mind another questionm, what is this
b_table.(myVars{iVar}) called/doing?
Thanks again! You've saved me hours and hours of messing around.
Jeff Miller
Jeff Miller 2021 年 2 月 13 日
You are welcome. I don't know what it is called, but it is one of my favorite MATLAB features. (sString) can be used after dots with structured types, and it means something like "use the field named by sString here as if I had typed sString in the program code itself".

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2021 年 2 月 12 日

コメント済み:

2021 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by