Extracting a range from a cell array.

28 visualizaciones (últimos 30 días)
Erwin Werkhoven
Erwin Werkhoven el 1 de En. de 2022
Editada: Stephen23 el 3 de En. de 2022
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 comentarios
Stephen23
Stephen23 el 1 de En. de 2022
Editada: Stephen23 el 1 de En. de 2022
"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}]
Erwin Werkhoven
Erwin Werkhoven el 1 de En. de 2022
Ok thanks for the explanation!

Iniciar sesión para comentar.

Respuesta aceptada

DGM
DGM el 1 de En. de 2022
Editada: DGM el 1 de En. de 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 comentarios
Erwin Werkhoven
Erwin Werkhoven el 3 de En. de 2022
Editada: Erwin Werkhoven el 3 de En. de 2022
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 el 3 de En. de 2022
Editada: Stephen23 el 3 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by