Storing data in Table inside for loop

조회 수: 2 (최근 30일)
Seum Bin Rahman
Seum Bin Rahman 2019년 10월 8일
답변: Jon 2019년 10월 8일
I wish to create a table where one row will be added in each for loop iteration. The caclulation is correct, but I cannot store them in a table. Can I get any suggestions?
clc
clear
raw=readtable('Small.xlsx');
model=readtable('Models.xlsx');
problem=readtable('Problems.xlsx');
raw_model=raw.Models;
raw_problem=raw.Problems;
M=height(model);
P=height(problem);
for j=1:1:M
reference1=model{j,1};
A = raw(strcmp(raw_model,reference1),2);
for i=1:1:P
reference2=problem{i,1};
B = A(strcmp(A{:,1},reference2),1);
quantity=height(B);
C = [reference1,reference2,quantity]
T(i,:)=table(C)
%above line is re-written in each iteration. I wish to store data C for all iteration
end
end

채택된 답변

Jon
Jon 2019년 10월 8일
Looking quickly at your loop structure, it seems that if you want a row for every iteration you must count how many times you have reached the assignment statement. So you could define a counter before entering the outer loop call it iCount, and then increment it each time you reach the assignment statement and use that for indexing into the table you are creating. So something like
% initialize counter
iCount = 1;
for j=1:1:M
reference1=model{j,1};
A = raw(strcmp(raw_model,reference1),2);
for i=1:1:P
reference2=problem{i,1};
B = A(strcmp(A{:,1},reference2),1);
quantity=height(B);
C = [reference1,reference2,quantity]
T(iCount,:)=table(C)
% increment counter
iCount = iCount + 1;
end
end

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by