Fill table of NaNs with data though loop

2 views (last 30 days)
Hello all,
I have a table of NaNs "data" with 100 rows and 6 columns. In a for loop i'm doing some calculations and in every loop get a 10x6 table. I now want to fill my initial Nan table "data" with the new data after every loop, i.e. start with rows 1-10, then 11-20 and so on.
I know this sounds very simple but I am definitely stuck. Also, I'm aware that a table of NaNs is probably not the most prominent way to handle this...
Here's a mwe:
col = NaN(100,1);
allData = table(col,col,col,col,col,col,'VariableNames',{'....'})
[my loop...]
data = [data ; new_rows] %This just appends the data to my Nan table which I don't want
I would be very thankful for suggestions and hints! Thank you

Accepted Answer

Guillaume
Guillaume on 30 Jul 2019
It's not clear what you're doing in the loop. Possibly, you should be using rowfun, groupsummary or similar instead of an explicit loop.
If I understood correctly, with your explicit loop:
for row = 1:10:height(allData) %what is the loop looping over?
%... calculations that produce new_rows, a table with 10 rows and the same variables as allData
allData(row:row+9, :) = new_rows;
end
  5 Comments
Guillaume
Guillaume on 31 Jul 2019
I'm sorry but I don't understand what you mean by When I declare alldata in the beginning how do I make sure that it doesn't change to the initial NaN table after every loop? Do I have to save the results in a fdifferent structure?
The best way to clarify is to show the code you are actually using.
If the table doesn't actually exist before your loop and the whole purpose of the loop is to construct it pieces by pieces, then a better approach may be to store each piece in a cell array and concatenate the whole lot at the end:
tables = cell{numsteps, 1};
for step = 1:numsteps
%some code that create a table called new_rows
tables(step) = new_rows
end
finaltable = vertcat(tables{:});
Pauli
Pauli on 1 Aug 2019
I got it now, using your first suggestion.Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!