From: Bernd Bumüller on 21 Mar 2010 19:15 Hello! I have list with parameters, with which I calculate a result which is in the end processed in my main file. Like this: [Code] function [a,b]=parameters; a=1; b=2; [/Code] [Code] function some=sum [a,b]=parameters; some=a+b; [/Code] %main file [Code] . . . someFunction(sum) %the result is processed here . . . [/Code] Now I wanted to use more than one list of parameters which only needed to be passed to sum.m . Somewhere I read that this is done with function handles. So I tried this: [Code] function [a,b]=parameters2; a=3; b=4; [/Code] function some=sum(function_handle) lalala=(a)function_handle; [a,b]=lalala; some=a+b; %main file [Code] . . . someFunction(sum(parameters2)) . . . [/Code] But this didn't work.Can anyone please post some correct code for this? Thank you.
From: Jan Simon on 21 Mar 2010 20:23 Dear Bernd! > Now I wanted to use more than one list of parameters which only needed to be passed to sum.m . Somewhere I read that this is done with function handles. So I tried this: > > function [a,b]=parameters2; > a=3; > b=4; > function some=sum(function_handle) > lalala=(a)function_handle; > [a,b]=lalala; > some=a+b; > someFunction(sum(parameters2)) > But this didn't work. It is always redommended not to claim "this does not work", but post the exact error message and the corresponding line of code. Nobody can run your code, so it is impossible to guess, what exactly happens. You could try this: Call your function SUM with a function handle: someFunction(sum(@parameters2)) Then the "lalala" is not needed. But it could be easier to apply your calling method and use VARARGIN to handle a different number of inputs. BTW: Do not use the name of builtin functions as "sum" as names for functions or variables. You can find a lot of postings here, which have been solved by avoiding these names. Good luck, Jan
From: Bernd Bumüller on 22 Mar 2010 08:27 "Jan Simon" wrote > It is always redommended not to claim "this does not work", but post the exact error message and the corresponding line of code. Nobody can run your code, so it is impossible to guess, what exactly happens. > > You could try this: Call your function SUM with a function handle: > someFunction(sum(@parameters2)) > Then the "lalala" is not needed. > But it could be easier to apply your calling method and use VARARGIN to handle a different number of inputs. > > BTW: Do not use the name of builtin functions as "sum" as names for functions or variables. You can find a lot of postings here, which have been solved by avoiding these names. > > Good luck, Jan Hallo Jan, great answer, it worked with varargin. But i am still interested in how it should look like with function handles. Your suggestion: [Code] function some=anyfun(function_handle) [a,b]=function_handle; some=a+b; [/Code] [Code] someFunction(anyfun(@parameters2)) [/Code] brings the error message: anyfun.m, line 2, column 5 "function_handle" was previously used as a variable, conflicting with its use here as the name of a function I would be happy about any new suggestions!
From: Steven Lord on 22 Mar 2010 09:17 "Bernd Bumüller" <leiwies(a)gmx.de> wrote in message news:ho7nmm$qvh$1(a)fred.mathworks.com... > "Jan Simon" wrote >> It is always redommended not to claim "this does not work", but post the >> exact error message and the corresponding line of code. Nobody can run >> your code, so it is impossible to guess, what exactly happens. >> >> You could try this: Call your function SUM with a function handle: >> someFunction(sum(@parameters2)) >> Then the "lalala" is not needed. >> But it could be easier to apply your calling method and use VARARGIN to >> handle a different number of inputs. >> >> BTW: Do not use the name of builtin functions as "sum" as names for >> functions or variables. You can find a lot of postings here, which have >> been solved by avoiding these names. >> >> Good luck, Jan > > Hallo Jan, > great answer, it worked with varargin. But i am still interested in how it > should look like with function handles. Your suggestion: > > [Code] > function some=anyfun(function_handle) > [a,b]=function_handle; > some=a+b; > [/Code] > [Code] > someFunction(anyfun(@parameters2)) > [/Code] > > brings the error message: anyfun.m, line 2, column 5 > "function_handle" was previously used as a variable, > conflicting with its use here as the name of a function > > I would be happy about any new suggestions! Bernd, I _think_ I understand what you're trying to do, but if I'm wrong you should start at the beginning and explain what exactly you're trying to accomplish. Your original posting, when I reread it, sounds more like you have decided upon an approach and want to know how to implement it, but there may be an alternate approach that's easier to understand, more robust, and/or more efficient. If you want to invoke a function handle with 0 inputs, you need to call it using empty parentheses: function some = anyfun(function_handle) [a, b] = function_handle(); -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Bernd Bumüller on 22 Mar 2010 13:44 Thank you, it was the parentheses. Now it is working.
|
Pages: 1 Prev: fmincon, Hessian and extra parameters Next: How to have drop down menu to select in inputdlg |