Fill table of NaNs with data though loop

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

 채택된 답변

Guillaume
Guillaume 2019년 7월 30일

0 개 추천

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

Pauli
Pauli 2019년 7월 31일
편집: Pauli 2019년 7월 31일
I think that might be what I'm looking for but I get the error message "Conversion to double from cell is not possible." Tried curly braces but it doesn't change anything.
Also, what do you mean by not using an explicit loop?
Guillaume
Guillaume 2019년 7월 31일
You haven't given enough details to understand what type your variables are. It's not even clear what you iterate over, so there's a lot of guessing involved.
In the above, alldata is a table (as you've shown it being created as such). The conversion to double error would imply it's a double array instead. As I wrote in the comment, the assumption is that new_rows is also a table with the same variable names (otherwise, you wouldn't be able to vertically concatenate it with a table in your example code), the error message implies it's a cell array. There appears to be a mismatch between your explanation and example and what you actually have.
As for not using an explicit loop, as I said, it's possible that rowfun, groupsummary, maybe splitapply could replace it and make the whole thing simpler. Again, more information needed about what you're doing.
Pauli
Pauli 2019년 7월 31일
Sorry this is so wishy-washy. In the loop I'm going thourgh several folders and files and the output is each time a 10x6 table. alldata as well as new_rows are tables, found the mistake now.
One more question: 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?
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 2019년 8월 1일
I got it now, using your first suggestion.Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2019년 7월 30일

댓글:

2019년 8월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by