cellfun for objects
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (9)
Sean de Wolski
le 25 Août 2011
1 vote
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 commentaires
Jan
le 25 Août 2011
+1: It is obvious, that the strange EVAL method causes problems. The additional abstraction layer of calling the class indirectly increases the confusion level.
Junior
le 25 Août 2011
Sean de Wolski
le 25 Août 2011
What about writing everything in the cell to an mfile with fprintf, and then running the mfile?
Junior
le 25 Août 2011
Sean de Wolski
le 25 Août 2011
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
le 25 Août 2011
Junior
le 25 Août 2011
Jan
le 25 Août 2011
0 votes
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 commentaires
Junior
le 25 Août 2011
Fangjun Jiang
le 25 Août 2011
cellfun() does the function specified on every element of the cell array.
Fangjun Jiang
le 25 Août 2011
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 commentaires
Sean de Wolski
le 25 Août 2011
Not with me! (Well I tried to fool it)
*SAVE EVERYTHING BEFORE YOU RUN THIS, IT CAUSES CLOSURE*
sc = {'clear';'A=magic(4)';'B=A*rand(4,1)'}
cellfun(@eval,sc)
Junior
le 25 Août 2011
Sean de Wolski
le 25 Août 2011
I think you're digging yourself into a mighty deep hole with that one. Why don't you describe the overall goal to us, maybe we can point you somewhere else.
Fangjun Jiang
le 25 Août 2011
Yes, because the command in your script could return any size of data, you have to use the (...'uni',false) option and the results are stored in a cell array. You have to do whatever necessary to get the format you want.
Fangjun Jiang
le 25 Août 2011
@Sean. Yes, your example crashed my MATLAB too!
@Junior, please explain your goal and there might be better option.
Junior
le 25 Août 2011
Sean de Wolski
le 25 Août 2011
That's not so bad. For each textfile create:
-a directory
-a driver script to drive the mfiles
-the various mfiles.
Fangjun Jiang
le 25 Août 2011
@Junior, Okay. It sounds like you've worked it through using cellfun() and your class. My understand is that you need to read original data from text file and assign them to the attributes of your object. If class/object is not required, it might be a good idea to use structure. See help struct
Junior
le 25 Août 2011
Sean de Wolski
le 25 Août 2011
At a few kilobytes per file it may make it to a megabyte at some point :)
Jan
le 25 Août 2011
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 commentaire
Junior
le 25 Août 2011
Daniel Shub
le 26 Août 2011
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 commentaires
Junior
le 26 Août 2011
Junior
le 26 Août 2011
Sean de Wolski
le 26 Août 2011
I agree with Daniel. One of the reasons we recommend against using eval is that it easily and reliably causes bugs that are very difficult to locate and debug. You already seem to be having that problem. As long as you're reluctant to look into other more standard solutions, you'll continue to have the problem and likely be unable to solve it.
Also, why bother using cellfun? Might as well just use a for-loop, they're just as fast (faster on my system actually) and give you more options.
Daniel Shub
le 26 Août 2011
What about replacing script with a bunch of anonymous function handles that have the footprint @(a,b,...,z)().
script={@(a,b,...,z)(x+y), @(a,b,...,z)(a-b)}
I think this would avoid the eval and might be less error prone.
Junior
le 26 Août 2011
Sean de Wolski
le 26 Août 2011
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
le 26 Août 2011
Junior
le 26 Août 2011
Junior
le 26 Août 2011
Junior
le 28 Août 2011
4 commentaires
Daniel Shub
le 28 Août 2011
and yet you decided not to accept my answer or even given me a vote :(
Junior
le 28 Août 2011
Walter Roberson
le 29 Août 2011
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
le 30 Août 2011
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.
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!