Generate comma separated list in single line of code?
10 views (last 30 days)
Show older comments
I would like to put a piece of code within an expression which generates a comma separated list, without having to create a dummy variable. Is this possible? I was trying to use subsref(), like this:
subsref(repmat({'A'},1,4),struct('type','{}','subs',{{':'}}))
But this only returns a single output. It seems like subsref can't actually behave like A{:} . Is there another way?
The specific reason I would like to do this is for indexing an array where the dimension is unknown prior to runtime. So instead of (for example)
idx = [{1} repmat({':'},1,ndims(inputData)-1)];
output = A(idx{:});
I could do something like
output = A(<expression>);
8 Comments
Adam
on 13 Jun 2017
Edited: Adam
on 13 Jun 2017
It matters more whether there is a clear and useful answer than whether the initial question might confuse. If it were a question where the answers just add to the confusion then it would be bad, but if the accepted answer (accepting Stephen's answer would be useful!) clears up the confusion then that is fine.
Hence, I removed the flag from the question.
Accepted Answer
Stephen23
on 13 Jun 2017
Edited: Stephen23
on 13 Jun 2017
"I would like to put a piece of code within an expression which generates a comma separated list, without having to create a dummy variable. Is this possible?"
No.
Read this answer and the comments below for the reasons why.
"But this only returns a single output."
No, actually your code works perfectly and returns all of the elements of the cell array, just as we would expect. You just didn't define output variables for all of the outputs of the function:
>> Z = {'A','B','C','D'};
>> [a,b,c,d] = subsref(Z,struct('type','{}','subs',{{':'}}))
a =
A
b =
B
c =
C
d =
D
Because subsref is a perfectly normal function its outputs are only allocated to multiple output variables when those variables are defined in a comma-separated list as output arguments. Exactly like any other function, we would not expect
max(X)
to return both of its output arguments until we have defined both of them in a comma-separated list:
[val,idx] = max(X)
Exactly the same applies to subsref, because it is a normal function as well, and is consistent with what the documentation states regarding returning multiple function outputs:
This all begs the question: what is {:} if not a function? Answer: I have no idea.
Summary: your question is basically the wrong way around. You asked about why subsref behaves strangely, whereas in fact subsref behaves perfectly normally for a MATLAB function. It is actually {:} that behaves strangely: you should be asking about that!
3 Comments
More Answers (3)
Wonsang You
on 13 Jun 2017
Please try the following command.
subsref(repmat({'A'},1,4),struct('type','()','subs',{{':'}}))
1 Comment
Stephen23
on 13 Jun 2017
Edited: Stephen23
on 13 Jun 2017
This answer is irrelevant to the topic at hand: it does not return a comma-separated list as the original question asks about, and even the title clearly states: "Generate comma separated list.."
This code simply returns a cell array, and is equivalent to (:).
The original question asks about generating a comma-separated list, equivalent to {:}.
Someone apparently does not know the difference, and voted for it. Perhaps they liked the pretty output.
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!