Extracting a range from a cell array.

Hi,
Consider this example:
a = ["a", "b", "c", "d", "e"]
b = a(3:end)
x = {"a", "b", "c", "d", "e"}
y = x(3:end)
z = x{3:end}
It gives the following output:
a =
1×5 string array
"a" "b" "c" "d" "e"
b =
1×3 string array
"c" "d" "e"
x =
1×5 cell array
{["a"]} {["b"]} {["c"]} {["d"]} {["e"]}
y =
1×3 cell array
{["c"]} {["d"]} {["e"]}
z =
"c"
I'd expect that z = x{3:end} would give the same 1x3 string array as b. Why does it only extract element "c"?
And how can I get result b (the 1x3 string array) from x?
Thanks in advance.

2 Comments

"I'd expect that z = x{3:end} would give the same 1x3 string array as b."
Nope, that is not how curly braces work with cell arrays: curly braces do NOT automagically concatenate anything together (luckily, otherwise we would be very limited in how we could use cell arrays containing different data types).
"Why does it only extract element "c"?"
Because you are generating a comma-separated list from the contents of the cell array:
"And how can I get result b (the 1x3 string array) from x?"
SImply concatenate the scalar strings (in your comma-separated list) into one string array:
[x{3:end}]
Ok thanks for the explanation!

Sign in to comment.

 Accepted Answer

DGM
DGM on 1 Jan 2022
Edited: DGM on 1 Jan 2022
How about
x = {"a", "b", "c", "d", "e"}
x = 1×5 cell array
{["a"]} {["b"]} {["c"]} {["d"]} {["e"]}
z = horzcat(x{3:end})
z = 1×3 string array
"c" "d" "e"
% or just
z = [x{3:end}]
z = 1×3 string array
"c" "d" "e"
To clarify, the expression x{3:end} has three outputs. When only a single output is requested, you'll only get the first one of the three.

5 Comments

Ah ok, with z = [x{3:end}] you merge the separate outputs into an array again. Bit confusing that the {} notations responds differently than the [] notation, but now I understand. Thanks a lot!
Ok so apparenly this works if x contains string objects (x = {"a", "b", "c", "d", "e"}) but if x contains character vectors (x = {'a', 'b', 'c', 'd', 'e'}) your solution z = [x{3:end}] results in 'cde' instead of the required ['c', 'd', 'e']. Do you know how to get the required result?
"Do you know how to get the required result?"
Your example "...results in 'cde' instead of the required ['c', 'd', 'e']." does not make much sense because the square brackets are a concatenation operator, so your "required" example
['c', 'd', 'e']
is already exactly equivalent to
'cde'
Square brackets are not a "list" operator like in some other languages (MATLAB does not have a "list" type, which seems to be what you are trying to write). When you concatenate those three characters, you will get one character vector 'cde', so that is already the expected result of concatenation using square brackets.
If you want the characters to remain somehow "separate" then you can either:
  1. keep them in a cell array (which is what you already have), or
  2. convert the entire cell array & content to a string array.
Ok thanks, so a = ["a", "b", "c", "d", "e"] gives an array (or vector, whatever it's called in matlab) of separate strings, but b = ['a', 'b', 'c', 'd', 'e'] merges everything into a single char vector 'abcde'. That's a bit confusing, but good to know.
Stephen23
Stephen23 on 3 Jan 2022
Edited: Stephen23 on 3 Jan 2022
Erwin Werkhoven: with MATLAB basically everything is an array:
Even scalar numbers are arrays:
X = 3
X is an array which has size 1x1(x1x1...). Square brackets always concatenate things together: this is exactly the same for every single array type (e.g. numeric, char, string, cell, struct). So this:
X = [1,2,3]
really just concatenates three 1x1 arrays into a 1x3 array.
The only thing you need to pay attention to, is what you are concatenating together. In both of your examples the separate string arrays and character arrays respectively are concatenated into larger string and character arrays. Absolutely no difference whatsoever, the square brackets always concatenate.
This is a 1x1 numeric array: 2
This is a 1x1 numeric array: 3
If we concatenate them together horizontally, we will get a 1x2 numeric array.
This is a 1x1 character array: 'a'
This is a 1x1 character array: 'x'
If we concatenate them together horizontally, we will get a 1x2 numeric array.
This is a 1x1 string array: "x"
This is a 1x1 string array: "hello world".
If we concatenate them together horizontally, we will get a 1x2 string array.
etc. etc for every other array type.
Note that the number of characters (i.e. content) does NOT determine the size of a string array (this also applies to other container array types too, e.g. cell arrays and structure arrays). Do not mix up the size of the content of a container array with the size of the container array itself:
The documentation clearly states that string is a container type.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 1 Jan 2022

Edited:

on 3 Jan 2022

Community Treasure Hunt

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

Start Hunting!