How can I rename a table within a for loop?

Hello everyone, I'm a novice in using tables with Matlab and I would like to have your help with my situation:
I have a very big table (about 277000 x 60) from which I need to extract all data related to a single variable. In particular I have a variable called 'chassi' that is a categorical array with 61 different categories. For each 'chassi' I need to create a new table in which I want to put all tha data related to date, latitude,longitude, ... related to that chassi. In order to do that in an "automated way" (since this code should be used even if the chassis are not 61 or if they change their ID), I'm using a for loop that should create, at every iteration, the new table that I need. The problem is that the code doesn't work in the most important line: the renaming line!
I put here the part of the code that I hope you could help me to fix.
% table TT already loaded before the loop
for i=1:length(num_telaio) %num_telaio is the categorical array containing the chassi numbers
lat_car = TT.Lat(TT.chassi==num_telaio(i));
lon_car = TT.Lon(TT.chassi==num_telaio(i));
date_car = TT.date(TT.chassi==num_telaio(i));
SOC_car = TT.BatPSOC(TT.chassi==num_telaio(i));
speed_car = TT.Speed(TT.chassi==num_telaio(i));
TRAJ = table(lat_car,lon_car,date_car,SOC_car,speed_car);
%code_that_rename_the_table_at_each_iteration = TRAJ;
end
Thanks for your help and sorry for my bad english
Matteo

2 Comments

code_that_rename_the_table_at_each_iteration
is commonly known as
dynamically_defining_variable_names_is_a_bad_way_to_write_code_no_matter_that_beginners_keep_wanting_to_do_it
Dynamically defining or accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Just use indexing (which is neat and efficient) or just keep the data in one table (which makes it trivial to operate on groups of data, unlike what you are trying to do).
Thank you Stephen, I will follow your suggestions.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2017a

Asked:

on 10 Apr 2019

Commented:

on 10 Apr 2019

Community Treasure Hunt

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

Start Hunting!