eval alternative for non-sequental variable names

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by