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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!