cellfun for objects

7 次查看(过去 30 天)
Junior
Junior 2011-8-25
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.

采纳的回答

Daniel Shub
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.

更多回答(9 个)

Sean de Wolski
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
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.
Junior
Junior 2011-8-25
The reason I don't do that is because the potential content of the m-file is part of some other object.

请先登录,再进行评论。


Junior
Junior 2011-8-25
Summarizing the steps:
lhs=(cellfun(@eval,script,'UniformOutput',false));
lhs=[lhs{:}];
lhs=reshape(lhs,size(script));

Jan
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 个评论
Junior
Junior 2011-8-25
I have no idea what cellfun does:-) it is a builtin function. I can successfully evaluate the content of each cell in script. myclass returns and object, which is like a structure.
Fangjun Jiang
Fangjun Jiang 2011-8-25
cellfun() does the function specified on every element of the cell array.

请先登录,再进行评论。


Fangjun Jiang
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 个评论
Junior
Junior 2011-8-25
@Fangjun, unfortunately structures are not enough here. I really need a class here.
@Sean, what you are suggesting is the option I had before and it may or may not be convenient. If I have say 100 files to read, I will then need as many directories, each one including many files.
Sean de Wolski
Sean de Wolski 2011-8-25
At a few kilobytes per file it may make it to a megabyte at some point :)

请先登录,再进行评论。


Jan
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.
  1 个评论
Junior
Junior 2011-8-25
Jan, what you suggest here would do with just eval as the content of each cell is a string. But the output of the evaluation process is an object of type "myclass". Gosh! I always forget that thing of running the loops backward. Thanks for reminding me.

请先登录,再进行评论。


Daniel Shub
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
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
Junior 2011-8-26
Dear Sean, I fully agree with that and I say it again I know how to do it that way. But I have also said that that is where I come from and I am trying to move away from that. This has consequences as you pointed out, but not necessarily the ones you are mentioning. If successful, the way I am doing things will be easiear for the users to understand what is going on and debug whatever is necessary.

请先登录,再进行评论。


Junior
Junior 2011-8-26
Hi Daniel, maybe you are onto something here. If I follow that route, I will have something like
script={'@(a,b,c,d)(a+b)'; '@(a,b,c,d)(c-d)'};
The content of each cell will have to be a string. The content of each cell is the result of parsing a file. Defining a string str0='@(a,b,c,d)';, each element of the cell array will have to be built as
script{i,j}=strcat(str0,string_ij);
where in this example, string_11='a+b';
Another problem is that the are some constants that need to be created before evaluating the script. Here is an example:
c1=1; c2=f(c1); c3=h(c2,c3); etc.
In the current implementation, typical entries of script look as follows
script={'x(1)+c1*x(2)+log(x(3))','x(4)-c3*exp(x(2)-x(3))'}
while c1, c2,...,cn are numbers, x on the other hand is a vector of elements of class myclass.

Junior
Junior 2011-8-26
I thought it might be instructive to see what the m-file would look like, the implementation i had earlier and that Sean is recommending.
function out=myfunc(x,a)
c1=f1(a);
c2=f2(a,c1);
c3=f3(a,c1,c2);
out(1,2)=myclass; % initialize output
out(1)=x(1)+c1*x(2)+log(x(3));
out(2)=x(4)-c3*exp(x(2)-x(3));
This is the typical function I would like to avoid writing to disk

Junior
Junior 2011-8-28
This is what I finally did, based on Daniel's advice.
script = {str2func('@(a,b,c,d)(a+b)'); str2func('@(a,b,c,d)(c-d)')};
lhs=cellfun(@(x)(x(a,b,c,d)),script,'UniformOutput',false);
lhs=[lhs{:}];
lhs=reshape(lhs,size(script));
with this, I got rid of eval altogether and it works fine.
  4 个评论
Walter Roberson
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
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 CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by