MATLAB Central Discussions - Join the conversation!
主要内容

Results for


Introduction
Comma-separated lists are really very simple. You use them all the time. Here is one:
a,b,c,d
That is a comma-separated list containing four variables, the variables a, b, c, and d. Every time you write a list separated by commas then you are writing a comma-separated list. Most commonly you would write a comma-separated list as inputs when calling a function:
fun(a,b,c,d)
or as arguments to the concatenation operator or cell construction operator:
[a,b,c,d]
{a,b,c,d}
or as function outputs:
[a,b,c,d] = fun();
It is very important to understand that in general a comma-separated list is NOT one variable (but it could be). However, sometimes it is useful to create a comma-separated list from one variable (or define one variable from a comma-separated list), and MATLAB has several ways of doing this from various container array types:
1) from a field of a structure array using dot-indexing:
struct_array.field % all elements
struct_array(idx).field % selected elements
2) from a cell array using curly-braces:
cell_array{:} % all elements
cell_array{idx} % selected elements
3) from a string array using curly-braces:
string_array{:} % all elements
string_array{idx} % selected elements
Note that in all cases, the comma-separated list consists of the content of the container array, not subsets (or "slices") of the container array itself (use parentheses to "slice" any array). In other words, they will be equivalent to writing this comma-separated list of the container array content:
content1, content2, content3, .. , contentN
and will return as many content arrays as the original container array has elements (or that you select using indexing, in the requested order). A comma-separated list of one element is just one array, but in general there can be any number of separate arrays in the comma-separated list (zero, one, two, three, four, or more). Here is an example showing that a comma-separated list generated from the content of a cell array is the same as a comma-separated list written explicitly:
>> C = {1,0,Inf};
>> C{:}
ans =
1
ans =
0
ans =
Inf
>> 1,0,Inf
ans =
1
ans =
0
ans =
Inf
How to Use Comma-Separated Lists
Function Inputs: Remember that every time you call a function with multiple input arguments you are using a comma-separated list:
fun(a,b,c,d)
and this is exactly why they are useful: because you can specify the arguments for a function or operator without knowing anything about the arguments (even how many there are). Using the example cell array from above:
>> vertcat(C{:})
ans =
1
0
Inf
which, as we should know by now, is exactly equivalent to writing the same comma-separated list directly into the function call:
>> vertcat(1,0,Inf)
ans =
1
0
Inf
How can we use this? Commonly these are used to generate vectors of values from a structure or cell array, e.g. to concatenate the filenames which are in the output structure of dir:
S = dir(..);
F = {S.name}
which is simply equivalent to
F = {S(1).name, S(2).name, S(3).name, .. , S(end).name}
Or, consider a function with multiple optional input arguments:
opt = {'HeaderLines',2, 'Delimiter',',', 'CollectOutputs',true);
fid = fopen(..);
C = textscan(fid,'%f%f',opt{:});
fclose(fid);
Note how we can pass the optional arguments as a comma-separated list. Remember how a comma-separated list is equivalent to writing var1,var2,var3,..., then the above example is really just this:
C = textscan(fid,'%f%f', 'HeaderLines',2, 'Delimiter',',', 'CollectOutputs',true)
with the added advantage that we can specify all of the optional arguments elsewhere and handle them as one cell array (e.g. as a function input, or at the top of the file). Or we could select which options we want simply by using indexing on that cell array. Note that varargin and varargout can also be useful here.
Function Outputs: In much the same way that the input arguments can be specified, so can an arbitrary number of output arguments. This is commonly used for functions which return a variable number of output arguments, specifically ind2sub and gradient and ndgrid. For example we can easily get all outputs of ndgrid, for any number of inputs (in this example three inputs and three outputs, determined by the number of elements in the cell array):
C = {1:3,4:7,8:9};
[C{:}] = ndgrid(C{:});
which is thus equivalent to:
[C{1},C{2},C{3}] = ndgrid(C{1},C{2},C{3});
Further Topics:
MATLAB documentation:
Click on these links to jump to relevant comments below:
Dynamic Indexing (indexing into arrays with arbitrary numbers of dimensions)
Nested Structures (why you get an error trying to index into a comma-separated list)
Summary
Just remember that in general a comma-separated list is not one variable (although they can be), and that they are exactly what they say: a list (of arrays) separated with commas. You use them all the time without even realizing it, every time you write this:
fun(a,b,c,d)
Yes, I'm it.
45%
No, someone else knows more.
55%
6563 个投票

Several major updates have been introduced to Answers’ reputation system! The updates include a new User Levels system, a new Editor indicator, and updated badges series.

1. User Levels

User Levels have become a best practice for many community sites to adopt. They help build trust in the community and provide recognition to contributors. There are 10 levels in the system and the labels will display next to users’ names throughout MATLAB Answers and on your community profile. We hope to see more users climb the ladder and level up!

2. Editor Indicators

Becoming an Editor (upon earning 3,000 points) is a huge milestone in Answers. Therefore, we introduced the Editor indicator to show our appreciation. From the screenshot below, you will notice a user can have both a User Level and an Editor indicator.

3. Updated Badge Series

Based on our analysis of existing badges, we decided to introduce 2 new badges into existing series and retire an entire badge series.

  • The Knowledgeable badge series and the Thankful badge series now have 5 levels.
  • The Revival badge series has been archived and is no longer being awarded. If you earned one of these badges, it would still show up in your community profile.

You will find more information on Answers help page . If you have any questions, comments or feedback, free feel to leave a comment below.

Cody is a useful tool to practice MATLAB skills not only by solving the problems but also learn from each other’s solutions. Sometimes you see subpar solutions that are cheats and hacks. With the flagging feature we released recently, you can help us identify solutions that administrators, including Community Advisory Board members, can review and delete.

How to flag?

Flag Options - Only available on solutions

Skiing
35%
Skating (including hockey)
21%
Sledding (luge, bobsled, etc.)
10%
Curling
19%
Biathlon (skiing + shooting)
15%
640 个投票
MATLAB (Way to go!!! You rock!)
49%
Python (not from within MATLAB)
29%
Any variation of C
12%
Java, Javascript
4%
R, Ruby, Swift, Go, Scala, PHP, VB
2%
Other not mentioned, or mixture
4%
10577 个投票
Ambient | Atmospheric | Nature
9%
Classical | Jazz | Musicals
10%
Electronic | Dubstep | House | VGM
15%
Lo-fi | Chill | Coffee House
22%
Rock | Metal | Pop | Punk | Hip Hop
27%
Other | Podcasts | Nothing
17%
753 个投票
I only use it for homework problems
22%
1
5%
2-10
24%
10-100
28%
More than 100
12%
None yet (just started learning)
9%
6646 个投票
1 (just me)
8%
2-10
9%
11-100
8%
More than 100 (e.g. University)
65%
I can't even guess.
9%
8483 个投票
1 monitor/screen
42%
2
46%
3
9%
4 or more
3%
12173 个投票
Windows
74%
Apple, Mac, iPad
13%
Android (MATLAB Online)
4%
iPhone (MATLAB Online)
1%
Unix, Linux, Ubuntu, etc.
6%
11570 个投票

Every day, thousands of people ask questions on MATLAB Answers and many of these are about their code. Questions such as “How can I make this faster?”, “Why do I get this error message?” or “Why don’t I get the answer I expect?”. There’s often one crucial thing missing though – the code in question!

Most of the people who answer questions on MATLAB Answers are volunteers from the community. They are answering your questions for fun, to learn more about MATLAB or just because they like to be helpful. This is even true for people such as me who are MathWorks members of staff. It’s not part of my role to patrol the community, looking where I can help out. I do it because I like to do it.

Make it easier to help me help you.

Imagine you’re a volunteer, looking for something interesting to answer. What kind of questions are you more likely to dig into and help an anonymous stranger figure out?

In my case, I almost always focus on problems that I can easily reproduce. I rarely know the answer to any question off the top of my head and so what I like to do is start off with the problem you are facing and use the various tools available to me such as the profiler or debugger to figure it out. This is the fun of it all for me – I almost always learn something by doing this and you get helped out as a side effect!

The easier I can reproduce your issue, the more likely I am to get started. If I can’t reproduce anything and the answer isn’t immediately obvious to me I’ll just move onto the next question. One example that demonstrates this perfectly is a case where someone’s MATLAB code was running too slowly. All of the code was available so I could run it on my machine, profile it and provide a speed-up of almost 150x.

It's not always feasible or desirable to post all of your code in which case you need to come up with a minimal, reproducible example. What’s the smallest amount of code and data you can post that I can run on my machine and see what you see? This may be more work for you but it will greatly increase your chances of receiving an answer to your question.

General web search
75%
Specific web search for MLC content
12%
I search directly within MLC
3%
Combination of choices 2 & 3
9%
Neither (please leave a comment)
1%
813 个投票
My school supplies me as a student
63%
School bought it (prof, staff,etc.)
10%
I bought my own (student, home, etc
5%
My company/gov't/organization
17%
I'm using a free trial right now.
4%
Gift from someone (e.g. Mathworks)
1%
17288 个投票
Less than 1 hour
17%
1-2 hours
17%
2-3 hours
16%
3-4 hours
14%
More than 4 hours per day
36%
1174 个投票
White and gold
32%
Blue and black
42%
I can see it both ways, depending
16%
Not sure or something else/neither
10%
419 个投票
English
38%
Other European language
20%
East Asian (Chinese, etc.)
11%
Indian (any)
12%
Spanish
5%
Other (Arabic, etc.)
14%
1117 个投票
Within the past year
24%
1 - 5 years ago
38%
5.01 - 10 years ago
17%
10.01 - 21 years ago
12%
During the last century (1900's)
8%
1300 个投票
A full prof. version w/all TB's
30%
A toolbox (TB) you don't have yet
6%
MATLAB swag (t-shirt, cap, mug,etc)
30%
Vacation at Cleve's in Arizona
12%
Full web site moderator privileges
2%
Free training class
20%
972 个投票
R2021b (latest official release)
35%
R2020a - R2021a
30%
R2018a - R2019b
17%
R2016a - R2017b
9%
R2015b or earlier
8%
4244 个投票
Go to top of page