cellfun usage trouble
9 次查看(过去 30 天)
显示 更早的评论
I am having trouble using cellfun in a specific case.
For example I have the following code:
prefix={'IS','MI'};
IS_max = 5;
MI_max = 6;
If I run
eval([prefix{1},'_max'])
I get the expected result, 5.
If I run
cellfun(@(x) eval([x,'_max']),prefix)
I get an error that IS_max is undefined.
Any suggestions for how I can get the cellfun command to work? My desired output would be [5,6];
thanks very much,
-Brian
1 个评论
Oleg Komarov
2012-4-17
You cannot do that. http://www.mathworks.co.uk/support/tech-notes/1100/1103.html
采纳的回答
Kelly Kearney
2012-4-17
Try evalin instead, which forces the function to reference the base workspace (rather than the local one):
cellfun(@(x) evalin('base', [x,'_max']),prefix)
0 个评论
更多回答(1 个)
Jan
2012-4-18
Using eval or evalin to access variables is prone to errors, hard to debug and in reduces the speed substantially. This topic has been discussed repeatedly in this forum and a search is recommended.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!