Match cell array names with table names

33 visualizaciones (últimos 30 días)
Paul
Paul el 21 de Ag. de 2025 a las 13:17
Editada: Stephen23 el 22 de Ag. de 2025 a las 8:12
I have 20 tables with the following table names:
str =
{'W1' }
{'S1' }
{'W2' }
{'W3' }
{'S2' }
{'S3' }
{'W4' }
{'S4' }
{'S5' }
{'W5' }
{'S6' }
{'W6' }
{'S7' }
{'W7' }
{'S8' }
{'W8' }
{'W8' }
{'W10'}
{'S9' }
{'S10'}
The letters W and S refer traffic flow in the west and south directions. The str is ordered.
I want to analyze the interaction in pairs. For example, first analyze interactions between 'W1' and 'S1', next between 'S1' and 'W2', and so forth with my function called CF. Continuing with the example:
Calculate a new S1 = CF(W1,S1).
Next calcutale a new W2 = CF(S1,W2) and so forth.
  1 comentario
Stephen23
Stephen23 el 21 de Ag. de 2025 a las 18:08
Editada: Stephen23 el 22 de Ag. de 2025 a las 8:12
You forgot to tell us the most important information: how did you get twenty badly-named** variables into the workspace? Did you write them all out by hand? Did you LOAD them? The point at which you created those twenty vairables in the workspace is the best place to fix the bad data design, by e.g. LOADing into an output variable or indexing into one array withing a loop. But it all depends on how those arrays are created, which you have not told us.
**because forcing meta-data into variable names invariably leads users into writing slow, complex, inefficient code:

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 21 de Ag. de 2025 a las 14:13
Editada: Matt J el 21 de Ag. de 2025 a las 14:23
You shouldn't have 20 tables. You should have 20 struct fields,
Tables.W1=... %a table
Tables.S1=... %another table
and so forth. Then you can freely refer to them dynamically,
for i=1:numel(str)
for j=1:numel(str)
Tables.(str{i}) = CF( Tables.(str{j}), Tables.(str{i}) ) ;
end
end

Categorías

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

Productos


Versión

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by