eval alternative for non-sequental variable names
1 view (last 30 days)
Show older comments
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 Comments
Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!