eval alternative for non-sequental variable names
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!