Extracting a range from a cell array.
Ältere Kommentare anzeigen
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 Kommentare
"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
am 1 Jan. 2022
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!