How to store two tables resulting from a for loop

조회 수: 59 (최근 30일)
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017년 7월 13일
댓글: Daisy 2022년 3월 25일
Hi guys,
I have a for loop with two iterations, and each iteration results into a table.
I would like to know whether it is possible to store the two tables coming out of the for loop such that I do not get the table from the first iteration overwritten from the second one.
Thanks for your help in advance.

채택된 답변

per isakson
per isakson 2017년 7월 13일
편집: per isakson 2017년 7월 13일
Yes, with something like this
for jj = 1 : 2
T = something;
filename = sprintf( 'myfile_%d', jj );
writetable( T, filename )
end
if I understand your question correctly
  댓글 수: 1
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017년 7월 13일
Yes, that essentially what I want. Is there a way to 'save' the results without actually saving a .txt file? Like the same way you would do it in a vector:
for i=1:2
y(i)=i+2
end

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

추가 답변 (3개)

Elias Gule
Elias Gule 2017년 7월 13일
Ok, I now understand what you want.
numIters = 3;
tables = cell(numIters,1); % A cell array to store the tables
for index = 1 : numIters
t0 = table();
tables{index} = t0;
end

Elias Gule
Elias Gule 2017년 7월 13일
Try this.
t1 = table();
t1.Name = 'My Name';
t1.Age = 89;
data = table();
data.Name = 'Your Name';
data.Age = 15;
% Append data contained in the t1 table to the data table
data = vertcat(data,t1);
  댓글 수: 2
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017년 7월 13일
편집: Konstantinos Tsitsilonis 2017년 7월 13일
Thanks for your answer Elias.
This approach shows how to store results from a for loop in one table if I understand correctly.
However, my problem is that I want each iteration to store its results in separate tables. I.e. if the for loop has 3 iterations, there should be three output tables, etc.
Pappu Murthy
Pappu Murthy 2018년 5월 18일
the statement "data = vertcat(data,t1); produced and error. Could not concatenate the table variable 'Name' using VERTCAT.
Caused by: Error using vertcat Dimensions of arrays being concatenated are not consistent.

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


Jose Rego Terol
Jose Rego Terol 2019년 10월 19일
Hi!
This thread is being very useful but I cannot find the way to save my tables in a excel file.
Here is the code
for jj = 1 : length(theFiles)
Table_parameters_spike = table(T,Q_spike,I_max,AT_HW,AT_RT)
filename = sprintf( 'Spike_%d', jj );
writetable(Table_parameters_spike, filename)
end
I got 5 .txt files but my objective is to get 1 .xls file with all the tables (2x5) concatenate vertically
Thanks!
  댓글 수: 1
Daisy
Daisy 2022년 3월 25일
Hi! Did you ever manage to figure this out? I have the exact same question..

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by