# How to Create Multidimensional table in for loop

12 views (last 30 days)
Karthi Ramachandran on 3 Apr 2019
Trajectory =1:15;
%TrajDetail = zeros(328,4,15);
for i = 1:length(Trajectory)
TSPANtf = [ 0 t(i) ] ;
%TSPANtf2 = [ 0 t_corrected_2 ] ;
RelTol = 3.e-14 ; AbsTol = 1.e-16;
Options = odeset('RelTol',3.e-14,'AbsTol',1.e-16);
[tt,xx] = ode113(@(t,x) CRes3BP_xy(t,x,mu),TSPANtf,x0_corrected(i,1:4)',Options);
plot(xx(:,1),xx(:,2),'k',xx(:,1),-xx(:,2),'k')
pause(0.01) ;
hold on
axis equal
pos_x = xx(:,1);
pos_y = xx(:,2);
vel_x = xx(:,3);
vel_y = xx(:,4);
TrajDetail (i)= table(pos_x,pos_y,vel_x,vel_y); %here the comes as (see below the italised stuff)
end
% I have not taken the tt values out but still i need
what I wanted to do is get the values of xx and tt for each iteration and store in a table named TrajDetail 1,TrajDetail 2 ...TrajDetail 15.
Note that xx changes the row size according to "tt"
eg: Iteration 1 may cause xx to have 160 x 4 ; Iteration two may have 200 x 4 etc
table should become like Table1 (ieTrajDetail 1) containg
tt;pos_x;pos_y:vel_x;vel_y and has to be done for each iteration
Error for above
Subscripting a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row subscript and a variable
subscript.
##### 1 CommentShowHide None
Rik on 3 Apr 2019
Tables are 2D in Matlab, containing numbered rows and named columns. If you want something else, you can't use a table. You could use a double array to create a single large multidimensional array, or encapsulate it in a cell array.

Rik on 3 Apr 2019
Edited: Rik on 3 Apr 2019
A bold guess based on your commented code:
Trajectory =1:15;
TrajDetail = zeros(1,4,15);
for i = 1:length(Trajectory)
TSPANtf = [ 0 t(i) ] ;
%TSPANtf2 = [ 0 t_corrected_2 ] ;
RelTol = 3.e-14 ; AbsTol = 1.e-16;
Options = odeset('RelTol',3.e-14,'AbsTol',1.e-16);
[tt,xx] = ode113(@(t,x) CRes3BP_xy(t,x,mu),TSPANtf,x0_corrected(i,1:4)',Options);
plot(xx(:,1),xx(:,2),'k',xx(:,1),-xx(:,2),'k')
pause(0.01) ;
hold on
axis equal
TrajDetail (1:size(xx,1),:,i)=xx(:,1:4);
end
Karthi Ramachandran on 9 Apr 2019
Thank you both Rik and Peter.

### More Answers (1)

Guillaume on 3 Apr 2019
Edited: Guillaume on 3 Apr 2019
You could store your 15 tables into a cell array,
Trajectory = 1:15;
TrajDetail = cell(size(Trajectory));
for i = 1:numel(Trajectory) %I recommend using numel instead of length
%... your code as is
TrajDetail{i} = table(pos_x,pos_y,vel_x,vel_y);
end
However, you may be better of using just one table with an extra column indicating which iteration the trajectory came from:
Trajectory = 1:15
TrajDetail = [];
for i = 1:numel(Trajectory)
%... your code as is
trajindex = repmat(i, size(pos_x, 1), 1);
TrajDetail = [TrajDetail; table(trajindex, pos_x,pos_y,vel_x,vel_y)]
end
By the way, instead of
pos_x = xx(:,1);
pos_y = xx(:,2);
vel_x = xx(:,3);
vel_y = xx(:,4);
something = table(pos_x,pos_y,vel_x,vel_y);
You could just write:
something = array2table(xx, 'VariableNames', {'pos_x', 'pos_y', 'vel_x', 'vel_y'});
##### 2 CommentsShowHide 1 older comment
Guillaume on 3 Apr 2019
I assume that error comes from my 2nd example, where I store everything in just one table. I've fixed a typo in the repmat of that code.
You certainly can't get that error from my 1st example.

R2018b

### Community Treasure Hunt

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

Start Hunting!