How do I remove "nested/deeper" layers of a cell?

26 次查看(过去 30 天)
Hi,
I have a cell called new_mat that has 2 levels (if I double click on a cell then another cell with the same value opens). Is it possible to remove the last/deepest layer somehow?
Thanks!

回答(1 个)

Jan
Jan 2022-12-12
You have posted a file, which contains the cell matrix called "new_mat". It does not have any second level. Therefore you cannot remove such a level also.
data = load('new_mat.mat')
% data =
% struct with fields:
% new_mat: {5×5 cell}
new_mat = data.new_mat;
  2 个评论
lil brain
lil brain 2022-12-13
I think I mis-worded my question @Jan. I was wondering if it is possible to not make it so that each cell contains a scalar but rather just the value itelf?
Stephen23
Stephen23 2022-12-13
编辑:Stephen23 2022-12-13
"I was wondering if it is possible to not make it so that each cell contains a scalar but rather just the value itelf?"
What exactly does that mean? By definition, a scalar numeric is just a single value.
It might make sense to you, but you will have to explain using some other words, of what you actually want to achieve (not in terms of concepts that already have meanings in MATLAB and mean basically the same thing).
All cells (except one) of your cell array contain scalar numerics which by definition are single values:
S = load('new_mat.mat')
S = struct with fields:
new_mat: {5×5 cell}
C = S.new_mat
C = 5×5 cell array
{[ 2.9473]} {[ 0.7736]} {[24.7335]} {[-32.1028]} {[ 5.4609]} {[ 7.9357]} {[15.6115]} {[28.3915]} {[ 51.8624]} {[ 1]} {[38.3376]} {[62.5463]} {[35.4955]} {[ 17.6059]} {[ 35.9168]} {[15.0732]} {[24.9668]} {[ 3.2505]} {[-21.6557]} {0×0 double} {[57.9756]} {[49.9486]} {[53.4301]} {[ 45.9361]} {[-17.1092]}
I am guessing that you actually want to create a numeric array from this cell array: this is possible only after replacing the empty cells with some scalar values (e.g. NaN):
C(~cellfun(@isscalar,C)) = {NaN};
M = cell2mat(C)
M = 5×5
2.9473 0.7736 24.7335 -32.1028 5.4609 7.9357 15.6115 28.3915 51.8624 1.0000 38.3376 62.5463 35.4955 17.6059 35.9168 15.0732 24.9668 3.2505 -21.6557 NaN 57.9756 49.9486 53.4301 45.9361 -17.1092
Note that this does not make "...so that each cell contains a scalar but rather just the value itelf" for the simple reason that it is not a cell array and does not have cells. Numeric arrays have elements, but not cells. Perhaps this was the cause of your confusing request.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by