eval alternative for non-sequental variable names

2 ビュー (過去 30 日間)
Jonathan
Jonathan 2015 年 2 月 19 日
編集済み: Jonathan 2015 年 2 月 19 日
I want to apply a function to all instances of workspace variables that match a pattern, and overwrite those workspace variables with the function output. Currently I do this with a for loop and eval(). Is there a better way?
This case is similar to, but not quite equivalent to some of the cases described in http://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html?refresh=true.
I'd prefer a solution that does not require exchanging the workspace variable definitions for struct field definitions. I am unwilling to replace my variable names with sequential ones as I need meaningful names.
Example:
% 2D variables
i_red = rand(10,20);
i_green = rand(10,20);
i_blue = rand(10,20);
% function to evaluate
raster_scan = @(im) reshape(permute(im,[2 1]),[numel(im) 1]);
% for loop with eval
inputs=whos('i_*');
inputs={inputs.name};
for i=1:length(inputs)
eval([inputs{i} '=raster_scan(' inputs{i} ');']);
end

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by