How to perform same operation on 100 different table in single loop
6 次查看(过去 30 天)
显示 更早的评论
I have 100 tables name as
Table1
Table2
Table3
..
Table100
and i want to perform same opration on each table; however, length of each table is different but has same number of column that is 2.
how can i perform same opration on each table and store results?
for i = 1:100
Data = Table('num2str('%c')',i);
end
error message:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched
delimiters.
1 个评论
Stephen23
2022-12-13
"I have 100 tables name as Table1, Table2, Table3,..Table100"
And that is the start of your problems.
Numbering variable names like that is a sign that you are doing something wrong.
Forcing meta-data (e.g. pseudo-indices) into variable names is a sign that you are doing something wrong.
Now that poor data design will force you into writing slow, complex, inefficient, obfuscated code which is buggy and difficult to debug. Read this to know some of the reasons why:
So far you have not told us how you got all of those variables into the MATLAB worskpace. Presumably you did not sit and write them all by hand... in any case, you should fix your data-design at that point, not by writing bad code later.
采纳的回答
Voss
2022-12-13
for i = 1:100
Data = eval(sprintf('Table%d',i));
% do something with Data ...
end
However, the better solution is not to end up with 100 tables called Table1, Table2, Table3, ...Table100, in the first place.
You can modify the code that gave you those 100 tables, to put them in a cell array instead, e.g., C = {Table1 Table2 ... Table100}.
Then the loop above would be:
for i = 1:100
Data = C{i};
% do something with Data ...
end
and you wouldn't really need the variable Data, you'd just refer to C{i} to use the ith table.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!