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

54 vues (au cours des 30 derniers jours)
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!!

Réponses (1)

Jeff Miller
Jeff Miller le 13 Fév 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 commentaires
Jeff Miller
Jeff Miller le 13 Fév 2021
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".

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by