Saving a new table for every loop iteration
4 次查看(过去 30 天)
显示 更早的评论
I am having trouble saving a function output in a for loop with a different variable name
variable_max = size(variable_scan,1);
for i = 1:variable_max
data_name = variable_scan{i,1};
column_name = variable_scan{i,2};
name = strcat('data_table',num2str(i));
name = dataextract(data,data_name,column_name);
end
dataextract outputs a table. I would like to save this output with something like table_'column_name'
1 个评论
Stephen23
2017-12-15
编辑:Stephen23
2017-12-15
"I am having trouble saving a function output in a for loop with a different variable name"
Yes, that is indeed a difficult thing to do. And also completely pointless because MATLAB has much neater, simpler, easier and more efficient methods of handling data.
Magically trying to create or access variable names is slow, complex, buggy, obfuscated, hard to debug, and insecure. In contrast using indexing is neat, simple, fast, easy to debug, and very efficient.
采纳的回答
KL
2017-12-15
Don't do that. Dynamic variable creation is a bad idea and totally unnecessary.
Use a cell array.
data = cell(variable_max,1);
column_name = cell(variable_max,1);
%and preallocate other variables, this is important if you want your code to be faster
for k=1:variable_max
...
...
column_name {k,1} = ...
data{k,1} = dataextract(...)
end
Now you have all your tables under one variable. You can access them by indexing.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!