merge multiple cells in only one

9 次查看(过去 30 天)
Hi! How can I merge these two cells?
Final results:
  3 个评论
Alberto Acri
Alberto Acri 2023-9-8
Hi @Dyuman Joshi! I've updated my question with file and final result!
Stephen23
Stephen23 2023-9-8
@Alberto Acri: note that storing scalar strings in cell arrays is very inefficient, and it avoids all of the benefits of using string arrays. You should be using string arrays, just as the documentation recommends:

请先登录,再进行评论。

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-9-8
As you want the final result to be a 4x1 cell array -
mat1 = load('value1.mat');
value1 = mat1.value1;
mat2 = load('value2.mat');
value2 = mat2.value2;
out1 = vertcat({value1},value2)
out1 = 4×1 cell array
{1×6 cell} {1×6 cell} {1×6 cell} {1×6 cell}
In case you want to store them as 4x6 as well -
out2 = vertcat(value1,vertcat(value2{:}))
out2 = 4×6 cell array
{["54"]} {["55"]} {["56"]} {["57"]} {["58"]} {["59"]} {["60"]} {["61"]} {["62"]} {["63"]} {["64"]} {["65"]} {["66"]} {["67"]} {["68"]} {["69"]} {["70"]} {["71"]} {["72"]} {["73"]} {["74"]} {["75"]} {["76"]} {["77"]}
  3 个评论
Dyuman Joshi
Dyuman Joshi 2023-9-9
Dynamically naming variables is not recommended - Why Variables Should Not Be Named Dynamically.
Store the data in an array and use indexing to access the data -
mat1 = load('value1.mat');
value1 = mat1.value1;
mat2 = load('value2.mat');
value2 = mat2.value2;
out1 = vertcat({value1},value2)
out1 = 4×1 cell array
{1×6 cell} {1×6 cell} {1×6 cell} {1×6 cell}
variable = vertcat(out1{:})
variable = 4×6 cell array
{["54"]} {["55"]} {["56"]} {["57"]} {["58"]} {["59"]} {["60"]} {["61"]} {["62"]} {["63"]} {["64"]} {["65"]} {["66"]} {["67"]} {["68"]} {["69"]} {["70"]} {["71"]} {["72"]} {["73"]} {["74"]} {["75"]} {["76"]} {["77"]}
Here, variable_k will correspond to the kth row of variable.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by