cellfun for objects
7 次查看(过去 30 天)
显示 更早的评论
Hi, I would like to run the following line of code
lhs=(cellfun(@eval,script));
script is a cell array and the content of each cell is a function of elements of a class called myclass. I get the following error message:
myclass output type is not currently implemented
Is there any workaround? Thanks, J.
0 个评论
采纳的回答
Daniel Shub
2011-8-26
I suggest replacing
script = {'a+b'; 'c-d'}
with
script = {@(a,b,c,d)(a+b); @(a,b,c,d)(c-d)};
You can do the conversion by doing something awful like:
script = cellfun(@eval, cellfun(@(x)(['@(a,b,c,d)(', x, ')']), script, 'UniformOutput', false), 'UniformOutput', false);
Note this does not require a, b, c, or d to be defined at the time of defining script. Rather, it just requires you to know the maximum number of objects in advance (here I stopped at 4, but you can use as many as you like).
With this version of script you can then define your objects
a = 1; b = 2; c = 3; d = 4;
and run script without eval:
y = cellfun(@(x)(x(a,b,c,d)), script);
Depending on your actual functions, you might need to set the UniformOutput flag to false.
0 个评论
更多回答(9 个)
Sean de Wolski
2011-8-25
I don't know if there is a way to make that work, but I can advise against doing it!
Don't Do It!
6 个评论
Sean de Wolski
2011-8-25
I'll bet you'll regret that later (3 months, 6 months, 2 years from now) when you want a record of what you did... Just a few thoughts from experience. Save the mfile with the date in the title if you have to, that'll make them easy to sort and and easy find.
Jan
2011-8-25
I have no idea, what you are doing. But when I read the message "myclass output type is not currently implemented", I do not think, that there is any workaround but implementing the output type.
2 个评论
Fangjun Jiang
2011-8-25
I am not sure about your class. But there is really no problem to make it work if you want it. You just need to make sure all the command scripts in the cell contents are valid (including arguments if any). Can you use a MATLAB class/object to provide an example to explain what you want?
script={'magic(4)','rand(4)'};
a=cellfun(@eval,script,'uni',0)
10 个评论
Jan
2011-8-25
CELLFUN is doing this (uniform output assumed):
function lhs = CellFun_M(Fcn, C)
for iC = numel(C):-1:1 % Backward for fast allocation
lhs(i) = feval(Fcn, C{i});
end
EVAL needs a string for the evaluation. But "the content of each cell is a function of elements of a class called myclass" sounds, like your cell (with the strange name "script") does not contain strings.
Unfortunately modern MATLAB releases do not contain the source code "cellfun.c" anymore. What a pitty. It was a nice piece of code for education.
Daniel Shub
2011-8-26
lhs=(cellfun(@eval,script));
I got to say that this looks like the absolute worse use of eval that I have ever seen. You say that "script is a cell array and the content of each cell is a function of elements of a class called myclass". I interpret this as meaning:
script = {'obj1([2,4,6]).methodA'; 'obj2([3,4,5]).methodB'};
If this is the case, you could probably parse the strings in script, and run them directly without eval. Or even better would be to run them before creating the string.
7 个评论
Sean de Wolski
2011-8-26
I've already recommended a way twice, and you agreed it would work! If you'd spent a fraction of the time that you've spent trying to debug something that might not even be possible you could probably have a fully automated application that given a batch of text files creates n directories with code to run each file and the results, and a driver script. You would then have a record of everything you've done, be able to use the full (very powerful) suite of MATLAB debugging tools, and have something that you or the next person to follow behind you stands a fighting chance of being able to figure out what you did in the future (this is a run-on sentence), there all of the advantages, and with the exception of having to take a few minutes at a chalk (or white) board to figure out an organized directory structure, none of the disadvantages.
Junior
2011-8-28
4 个评论
Walter Roberson
2011-8-29
You should accept the answer that got you the closest to what you needed to do. Very few people are going to be able to get you *exactly* to where you need to get as you live with program structures and variable names and time limitations and skill limitations and so on that you did not document here.
Daniel Shub
2011-8-30
I don't really care if you accept my answer, your answer, or someone else's answer. If the problem is solved, then please accept an answer.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Java from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!