how to return for loop tables to a table
1 view (last 30 days)
Show older comments
Hi All
I want to return two tables inside a for loop to the main table outside.
mat_bac is the main table outside the for loop.
Data_1 for i and data_2 for i+1 are the tables I need to return to mat_bac. So that at the end it's not overwritten.
Thank you.
I have a main table outside the for loop
frame par1 par2 par3
_____ ________ ________ ______
1 268.13 143.89 25.376
1 85.818 84.414 27.488
1 173.56 165.74 28.634
1 651.7 471.31 23.389
1 678.37 317.05 32.631
1 611.04 94.878 21.631
1 451.42 353.31 26.549
1 177.49 515.96 26.596
1 287.63 367.65 29.248
1 492.5 62.251 46.735
2 265.99 141.51 25.046
2 88.22 80.331 28.023
2 174.12 168.28 28.08
2 286.95 371.45 29.611
2 681.29 322.52 33.3
2 486.08 63.945 50.203
2 614.1 96.111 21.493
2 181.45 512.15 26.459
2 648.41 474.4 25.315
2 267.26 495.39 25.222
2 393.29 249.04 29.059
3 64.80742 146.0646 27.3122
3 290.8602 167.7889 35.7585
3 477.655 89.51805 21.64952
3 344.113 44.36785 44.03939
3 621.3208 408.272 24.24406
3 310.5134 329.8202 21.51631
3 72.62675 113.7154 23.65932
3 520.6362 404.6039 22.32997
4 289.5507 168.0988 35.5876
4 476.9346 91.47729 20.63287
4 656.3892 334.381 27.20086
4 65.7696 141.7556 27.21767
4 294.9997 330.3788 37.8143
4 549.6827 59.24164 22.09339
4 327.6798 52.62152 24.81
4 225.9563 516.5291 26.54855
4 264.0543 284.7716 36.37305
4 133.1544 124.9854 34.36764
Inside for loop i=1:4
for each iteration eg: i=1
data1
frame par1 par2 par3 par4
_____ ________ ________ ______ __
1 268.13 143.89 25.376 1
1 85.818 84.414 27.488 2
1 173.56 165.74 28.634 3
1 651.7 471.31 23.389 4
1 678.37 317.05 32.631 5
1 611.04 94.878 21.631 6
1 451.42 353.31 26.549 7
1 177.49 515.96 26.596 8
1 287.63 367.65 29.248 9
1 492.5 62.251 46.735 10
data2
frame par1 par2 par3 par4
_____ ________ ________ ______ __
2 265.99 141.51 25.046 1
2 88.22 80.331 28.023 2
2 174.12 168.28 28.08 3
2 286.95 371.45 29.611 9
2 681.29 322.52 33.3 5
2 486.08 63.945 50.203 10
2 614.1 96.111 21.493 6
2 181.45 512.15 26.459 8
2 648.41 474.4 25.315 4
2 267.26 495.39 25.222 11
2 393.29 249.04 29.059 12
now for i=1 i want to return data1 and dat2 to mat_bac, so that mat_bac has par4 included in it. likewise for i=2 ,3 ,4. my original table is much bigger than the mat_bac.
How can i do this?
Thank you
7 Comments
Bob Thompson
on 16 Aug 2019
'Not sure what data1 and data2 are but a table has to be regular in every variable must have the same number of rows.'
You cannot replace portions of mat_bac with data1 or data2 because of the comment above by dpb. Before you can even consider making these replacements you need to add the fifth variable to mat_bac for ALL rows. I would suggest doing this by creating a variable of the appropriate size and then something like the following:
mat_bac = addvars(mat_bac,var4,'After','var3');
After the fifth column has been added the replacement of values should be relatively simple, just a matter of indexing.
'Right now, i get only the last iteration value result.'
This sounds like a very different issue, likely something due to indexing. We would need to see the related part of your code to give you a more specific idea of how to fix it.
Answers (1)
Pujitha Narra
on 19 Aug 2019
Hi Karishma,
You can extract the “par4” data from “data1” and “data2” and store it as a matrix. Further use this matrix to add the variable “par4” to the table “mat_bac”.
Extracting data as a matrix:
%initialization
par4=[];
%inside the loop
par4=[par4; data1.par4];
par4=[par4; data2.par4];
Adding the new variable to “mat_bac”:
mat_bac=addvars(mat_bac,par4);
You can look at these links for further information:
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!