Is it possible to have an inline function inside a cell array to conditionally return an element to be stored?
Show older comments
Consider I have
mycondition = true;
truestatement = 'yes'; % returned by the inline function when mycondition is true
falsestatement = 'no'; % returned by the inline function when mycondition is false
mycell = {'a' , 'b' , <an inline function to create>};
is it possible to have an inline functrion inside the cell array so that it returns either truestatement or falsestatement without having to do this check outside the cell array and use extra variables?
I know there are different clear ways to do so, but I would like to understand if there is such a way.
Accepted Answer
More Answers (1)
Walter Roberson
on 1 Oct 2020
Edited: Walter Roberson
on 1 Oct 2020
No, but for different reasons.
You are wanting an inline function, when executed, that looks in the current workspace for the value of mycondition, and returns a value selected by it.
That would require that the inline function had no parameters.
However, the internal implementation of inline functions always requires that they accept at least one parameter. The function is not required to do anything with the parameter, but it needs to be passed.
There is no inline function equivalent to
f = @() [falsestatement(repmat(~mycondition,size(falsestatement))), truestatement(repmat(mycondition,size(truestatement)))];
f()
because you cannot implement that () empty parameter list for inline()
If you were willing to have the condition be passed in to the inline function instead of expecting it to be read out from the workspace, then that would be do-able -- provided that the values of falsestatement and truestatement and mycondition could be copied into the inline function instead of being referenced dynamically. But pulling out workspace values dynamically is hard for inline functions.
Why do you want to use inline() ? inline() has been recommended against for a long time.
7 Comments
Diaa
on 1 Oct 2020
Walter Roberson
on 1 Oct 2020
Anonymous functions rather than inline() .
inline() was always a hack.
Diaa
on 1 Oct 2020
Walter Roberson
on 2 Oct 2020
Okay, so let us make sure that we agree on what is being done:
You want to store a function handle in a cell array.
- is the function handle to be passed the value of mycondition?
- is the function handle to use the value of mycondition as of the time the function handle was defined?
- should the function handle dynamically look for the contents of the variables -- for example of the user changes the content of the variable truestatement after the function handle is created, then is the function handle to use the new value ? Should it do that only for mycondition and can assume that truestatement and falsestatement are fixed?
- is it acceptable that the function handle will have to be executed by your program in order to get the value?
Or, are you trying to create something like an Excel Macro, where even though what is stored at mycell{3} is the function handle, that any time after that that the code reads out the content of mycell{3}, you want MATLAB to automatically execute the function based upon the current value of mycondition, and give you the result? Like
mycondition = true;
disp(mycell{3}) %-> displays yes
mycondition = false;
disp(mycell{3}) %-> displays false
without the user needing to use
mycondition = true;
disp(mycell{3}()) %-> displays yes
mycondition = false;
disp(mycell{3}()) %-> displays false
??
If the requirement is that the value automatically reads out without the code having to explicitly execute the function handle, then that cannot be done with a cell array (but could be done by using your own object class.
Walter Roberson
on 2 Oct 2020
The version
mycondition = true;
disp(mycell{3}()) %-> displays yes
mycondition = false;
disp(mycell{3}()) %-> displays false
would be possible -- the () would trigger the execution of the function handle stored in that location and the function handle can examine the calling workspace value of mycondition and return appropriate value.
But if you do not trigger execution with () then when you access mycell{3} you would get back the function handle, rather than triggering execution of the function handle. You would need to define your own class of objects to get the kind of behaviour you are asking for.
I need to have an anonymous function working like the conditional (ternary) operator as in C language
The C ternary operator cannot store code fragments to be executed later. The C ternary operator can only evaluate at the time of the assignment into the location, and store the completed result there. You can, in C, store the pointer to a function, but in C the function will not be automatically executed when you ask to read the content of the location -- the most you could get back would be the pointer to the function.
Diaa
on 2 Oct 2020
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!