Match cell array names with table names

33 次查看(过去 30 天)
Paul
Paul 2025-8-21,13:17
编辑: Stephen23 2025-8-22,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 个评论
Stephen23
Stephen23 2025-8-21,18:08
编辑:Stephen23 2025-8-22,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:

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2025-8-21,14:13
编辑:Matt J 2025-8-21,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

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by