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