필터 지우기
필터 지우기

How can I run a for loop through the rows of a table?

조회 수: 364 (최근 30일)
Michael Ilardi
Michael Ilardi 2020년 8월 27일
편집: Leon 2024년 4월 24일
Hello,
I am trying to use two different tables.
Table 1 contains climate data. One variable is 'GHI' which tells me whether the sun is up. Another variable is 'outdoor temperature'.
Table 2 contains some variables I want to look up using the first table. Variables are 'outdoor temperature' and 'heating load'
I want to run a for loop that checks each row of table 1 to see if the sun is up ('GHI' > 0). If the sun is up, it checks the next value. IF the sun is not up ('GHI' = 0), then I want to take the outdoor temperature value from that row in table 1 and interpolate it into table 2 to get the heating value.
I want to do this for each row in Table 1, interpolating all the rows for when the sun is down, and add up the total heating loads using table 2.
This is as basic as I can explain it. I am not to familiar with MATLAB commands and syntax, but I have the logic down pretty well as I have described.
I do not want to confuse it by pasting any of my "code" because it doesn't work in the slightest and I am honestly not sure where to begin. All I have done is loaded the tables and named the variables.

답변 (1개)

Rohit Pappu
Rohit Pappu 2020년 8월 30일
As per my understanding of the question, you would like to iterate through the rows of a table.
A possible solution would be as follows,
%% Let table be T
table_size = T.size() ;
rows = table_size(1);
for row = 1:rows
%% To access a row in the table, use T(row,:)
end
Additional documentation about tables can be found here
  댓글 수: 4
RIchard
RIchard 2024년 4월 22일
Isn't 'column' your indexing variable? Outside of that, I don't think matlab allows for a non-retangular table. You have to have a 'x-by-y' table which means you have the same number of rows per column (or same number of columns per row).
Leon
Leon 2024년 4월 24일
편집: Leon 2024년 4월 24일
It's the actual column, like my_table.(ii) rather than being just the index ii. For example,
for column = my_table(:,:)
disp(column)
end
will display each column one by one, not just the index.
I don't want a non-rectangular table, I just wanted to know if I could access one row at a time without using an index. I ended up asking it as an actual question:

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by