Is it possible to have an inline function inside a cell array to conditionally return an element to be stored?

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

Hi,
The cell arrays in MATLAB are fundamental MATLAB classes/data types which have pre-defined definitions. The cell arrays do not support any such feature wherein you can add elements to it after defining an inline function within cell array initialization. To achieve the require purpose, you must add elements to cell array based on conditions that are defined outside the cell array.
Hope this helps.

More Answers (1)

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

Thanks for your clarification.
I actually am curious about extending my Matlab knowledge. So, I wondered if there is such an approach to dynamically and conditionally handle the cell array elements.
Ultimately, there is no real need for this but asking the experts here is always informative regardless the answer I receive.
Anonymous functions rather than inline() .
inline() was always a hack.
I was talking about the anonymous functions all the time. It is the first time now to hear about the inline() functions.
I am sorry about my bad wording.
So, by anonymous function, can my problem be addressed easily?
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.
  • is the function handle to be passed the value of mycondition?
Yes, I will pass three inputs: the value of the condition (true or false) and the true and false statements
  • is the function handle to use the value of mycondition as of the time the function handle was defined?
Yes
  • 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?
All the three imputs condition, true and false statements are fixed and executed before the creation of the anonymous function.
  • is it acceptable that the function handle will have to be executed by your program in order to get the value?
I am sorry but I don't quite understand what you mean here.
In general, I need to have an anonymous function working like the conditional (ternary) operator as in C language
statement to store in the cell array = Condition to test ? truestatement : falsestatement
For your last question, I need the cell array element to have the output of the function (i.e. truestatement or falsestatement) like this
mycondition = true;
disp(mycell{3}) %-> displays yes
mycondition = false;
disp(mycell{3}) %-> displays false
Even though it is not possible, I am thankful for your consideration and patience :)
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.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 16 Sep 2020

Commented:

on 2 Oct 2020

Community Treasure Hunt

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

Start Hunting!