Borrar filtros
Borrar filtros

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

364 visualizaciones (últimos 30 días)
Michael Ilardi
Michael Ilardi el 27 de Ag. de 2020
Editada: Leon el 24 de Abr. de 2024
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.

Respuestas (1)

Rohit Pappu
Rohit Pappu el 30 de Ag. de 2020
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 comentarios
RIchard
RIchard el 22 de Abr. de 2024
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 el 24 de Abr. de 2024
Editada: Leon el 24 de Abr. de 2024
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:

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by