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
Oleg Komarov 2012년 4월 17일
You cannot do that. http://www.mathworks.co.uk/support/tech-notes/1100/1103.html

댓글을 달려면 로그인하십시오.

 채택된 답변

Kelly Kearney
Kelly Kearney 2012년 4월 17일

2 개 추천

Try evalin instead, which forces the function to reference the base workspace (rather than the local one):
cellfun(@(x) evalin('base', [x,'_max']),prefix)

추가 답변 (1개)

Jan
Jan 2012년 4월 18일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by