Tondre Benjamin D
Tondre Benjamin D

Reputation: 1

Dynamic Variable assignment from table of tables

I’m trying to make dynamic variables, based on the names of columns of the tables of some data sets.

This is my Code:

varnammes = ["SpeedGoatT" "imu1_accel_x" "imu1_accel_y" "imu1_accel_z"  "imu1_gyro_x"   "imu1_gyro_y"   "imu1_gyro_z"   "imu1_orient_i" "imu1_orient_j" "imu1_orient_k" "imu1_orient_r" "imu2_accel_x"  "imu2_accel_y"  "imu2_accel_z"  "imu2_gyro_x"   "imu2_gyro_y"   "imu2_gyro_z"   "imu2_orient_i" "imu2_orient_j" "imu2_orient_k" "imu2_orient_r" "imu3_accel_x"  "imu3_accel_y"  "imu3_accel_z"  "imu3_gyro_x"   "imu3_gyro_y"   "imu3_gyro_z"   "imu3_orient_i" "imu3_orient_j" "imu3_orient_k" "imu3_orient_r" "imu4_accel_x"  "imu4_accel_y"  "imu4_accel_z"  "imu4_gyro_x"   "imu4_gyro_y"   "imu4_gyro_z"   "imu4_orient_i" "imu4_orient_j" "imu4_orient_k" "imu4_orient_r" "imu5_accel_x"  "imu5_accel_y"  "imu5_accel_z"  "imu5_gyro_x"   "imu5_gyro_y"   "imu5_gyro_z"   "imu5_orient_i" "imu5_orient_j" "imu5_orient_k" "imu5_orient_r" "imu6_accel_x"  "imu6_accel_y"  "imu6_accel_z"  "imu6_gyro_x"   "imu6_gyro_y"   "imu6_gyro_z"   "imu6_orient_i" "imu6_orient_j" "imu6_orient_k" "imu6_orient_r" "imu7_accel_x"  "imu7_accel_y"  "imu7_accel_z"  "imu7_gyro_x"   "imu7_gyro_y"   "imu7_gyro_z"   "imu7_orient_i" "imu7_orient_j" "imu7_orient_k" "imu7_orient_r"];
Tables = struct();
for i = 1:length(files)
    load(files(i))
    varName = sprintf('Table0%d', i-1); % Create a variable name
    Tables.(varName) = logsout.FileLogSignals{4}.Values;% Set the variable in the structure
    Tables.(varName) = timeseries2timetable(Tables.(varName));
    Tables.(varName) = timetable2table(Tables.(varName));
    Tables.(varName) = splitvars(Tables.(varName), 2);
    Tables.(varName).Properties.VariableNames(2:end) = varnammes;
    for x = 1:length(varnammes)
    % this is the problem:
    varName = [char(varnammes(x)) num2str(i-1)];
    eval([varName ' = x^2;']);
    %
    end
end

This is a test version that is making the variables, I want but instead of making them x^2, I want them to be the column that goes with the variable.

The normal code would be something like:

imu1_accel_x = Tables.Table00{:,3};

or:

imu1_accel_x = Tables.Table00.imu1_accel_x;

making the output being imu1_accel_x0, with all the values of column imu1_accel_x.

I tried making the eval line:

eval([varName ' = Tables.Table00.imu1_accel_x;']);

But I got an error and don't know why or how to make it work as intended.

Upvotes: 0

Views: 30

Answers (0)

Related Questions