How to output a for loop as a table with each iteration and result displayed

조회 수: 21 (최근 30일)
Jorge Manuel Ayala
Jorge Manuel Ayala 2021년 3월 11일
편집: Jorg Woehl 2021년 3월 11일
I have several tables called D2018, D2016,D2014...
For the example i will just use 2018 and 2019
I need a table with the mean of a variable ING for each year. Ive made a for loop:
list = ["2018", "2019"]
for i= list
file=strcat("D",i);
mean=varfun(@nanmean,eval(file),'InputVariables','ING')
year = str2double(i);
table(year,mean)
end
I dont know how to get each loop reasult in one table. I mean:
2018 mean2018
2019 mean2019
.
.
.
thanks

답변 (1개)

Jorg Woehl
Jorg Woehl 2021년 3월 11일
편집: Jorg Woehl 2021년 3월 11일
Hi Jorge,
First preallocate your table (outside the loop) according to the number of years in your list:
T = table('Size', [numel(list),2],...
'VariableTypes', {'double','double'},...
'VariableNames', {'year','mean'});
Inside the loop, use the following command to fill the table:
T(i,:) = {year, mean};
This will result in something like this (using rand numbers for the mean):
T =
4×2 table
year mean
____ _______
2018 0.48976
2019 0.44559
2020 0.64631
2021 0.70936

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by