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
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
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.
  2 个评论
AL
AL 2022-12-14
Hello,
Thank you so much. the code is working perfectly.
can you please tell me one more thing.
how can i store each table after performing opertaion on it?
right now only Data is overwrtitting everytime when the for loop is running.
Voss
Voss 2022-12-14
编辑:Voss 2023-3-14
Assuming you still have 100 tables called Table1, Table2, etc.:
for i = 1:100
Data = eval(sprintf('Table%d',i));
% do something with Data ...
% overwrite Table%d with the new value of Data:
eval(sprintf('Table%d = Data;',i));
end

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by