How to pop the content of a matrix in a way like pop the content of a cell
Show older comments
We know that the arguments of a function can be placed in a cell and call it like this:
aCell = {arg1,arg2,arg3};
myFunction(aCell{:});
Is there a way to do this for a vector instead?
I tried this way, but it does not work
aVector = [2 3 4 5];
myFun(aVector(:)) % this does not work, the argument is a column vector [2;3;4;5] instead.
Similarly,
aVec = [1 2 3];
aCell = {'d','dew',aVec(:)} % I would expect aCell is {'d','dew',1,2,3}, but it is {'d','dew',[1;2;3]},
Thanks.
2 Comments
KSSV
on 19 Jun 2020
Try num2cell(aVec)
There is no way to do this with a numeric array (or a char array, logical array, etc.). The only ways to generate a comma-separated list (the proper name for what you are trying to do) are from a cell array or from a field of a structure:
Accepted Answer
More Answers (2)
madhan ravi
on 19 Jun 2020
Edited: madhan ravi
on 19 Jun 2020
aVector = [2 3 4 5];
aVector = num2cell(aVector);
myFun(aVector{:}) % ;)
Walter Roberson
on 19 Jun 2020
1 vote
No there is not.
You can num2cell the vector and assign that to a variable and do expansion on that.
There are ways to use auxiliary anonymous functions to do the num2cell for you, and even to do cell expansion on the result, but when you proceed that way, the MATLAB parser will not know that multiple outputs are required and will pull out only the first value.
Categories
Find more on Whos 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!