modify all vectors in workspace

8 ビュー (過去 30 日間)
Joel Rovner
Joel Rovner 2016 年 4 月 18 日
編集済み: Stephen23 2019 年 6 月 19 日
I want to perform the same operation namely reshape() on all the arrays stored in the workspace, but there are a lot of arrays present and it would be tedious to do them individually. Is there any way to iterate through all of the arrays. For example I know if I type myvars=who; and then myvars(1) it will display the first array in the workspace. But how would you use myvars(i) in say reshape()?

回答 (2 件)

Stephen23
Stephen23 2016 年 4 月 19 日
編集済み: Stephen23 2019 年 6 月 19 日

Kirby Fears
Kirby Fears 2016 年 4 月 18 日
Joel,
I will preface this by saying you really shouldn't have variables floating around your workspace without knowing their names at all times. The best practice would be to store all variables inside a cell array or struct which you can easily iterate over.
Nonetheless, here's a way it can be done with 'who'.
vars = who;
for i = 1:numel(vars),
tempvar = eval(vars{i});
if isnumeric(tempvar),
% perform manipulations of tempvar, like reshaping
end
eval(sprintf('%s = tempvar;',vars{i}));
end
I strongly suggest you revisit the process that generated all these variables and store them in a struct or cell array instead.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by