Clear Filters
Clear Filters

I do not know how to make this work??

10 views (last 30 days)
Benjamin
Benjamin on 21 Jun 2024
Commented: Image Analyst on 21 Jun 2024
Im trying to make dynamic variables, based on the names of colums of the tables of some data sets.
This is my Code, the bolded section is my issue
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)
varName = [char(varnammes(x)) num2str(i-1)];
eval([varName ' = x^2;']);
end
end
This is a test version that is makeing the variables I want but instead of makeing 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;
makeing the output being imu1_accel_x0 , with all the values of column imu1_accel_x
I tried makeing the eval line:
eval([varName ' = Tables.Table00.imu1_accel_x;']);
but i got an error and dont know why or how to make it work as inteded
Ive sent an image of the output given the X^2 which works as intended.
Any assistance would be apreciated.

Answers (2)

Steven Lord
Steven Lord on 21 Jun 2024
Can you dynamically create variables with names like the ones you showed in your example? Yes.
Should you do this? The general consensus is no. That Discussions post explains why this is generally discouraged and offers several alternative approaches.
  4 Comments
Benjamin
Benjamin on 21 Jun 2024
thank you for responding tho
Image Analyst
Image Analyst on 21 Jun 2024
It's deceptive to call a structure array "Tables" when there is a table variable type.
It might help if we saw the other person's code to see what kind of data it expects. Does it want a table? A structure array? Or multiple structure arrays?

Sign in to comment.


Image Analyst
Image Analyst on 21 Jun 2024

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!