I do not know how to make this work??

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)

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

I understand that but I'm trying to use my tabled data in someone elses code and dont fully understand where they call each variable containing the data, so i was trying to settup the structure of my variables loosly in the workspace like his data points where so i could holpefully just run his code. I only attempted this originally as a method of reformatting the my data without line by line setting the variables, so that it will work on their code. So is there a way to make that line work calling the data just for the purposes of answering the question. Im trying to asssin the table data to the variables being created without doing it manually but at this point I'm probbaly just going to do it manually.
If i understood in thier code where i could just call a section of my tables then i would just do that but I currently don't and was just trying to make a simple solution around it.
thank you for responding tho
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.

Products

Release

R2024a

Asked:

on 21 Jun 2024

Commented:

on 21 Jun 2024

Community Treasure Hunt

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

Start Hunting!