Check if cell contains another cell

조회 수: 5 (최근 30일)
Marc Laub
Marc Laub 2022년 10월 20일
댓글: Jiri Hajek 2022년 10월 21일
Hey,
I need to check if my cell contains another cell or if the contant is a character vector.
So I got a
a=1x130 cell
and
a{1}='1-v1';
a{2}='.*'
a{3}='3+v2'
a{4}=1x3 cell
...
So e need to check wether the cell content is a character vector like a{1} or a{3}, contains an operater such as a{2}, or a cell itsself with the same structure as a
Then I wanna
eval('(1-v1).*(3+v2)')
So with
ischar
I can check wether its .* or an character arry, but how do I check if its a subcell, to call the fiunction recursive?
Best regards

채택된 답변

the cyclist
the cyclist 2022년 10월 20일
a{1}='1-v1';
a{2}='.*';
a{3}='3+v2';
a{4}={'this is a cell'};
cellfun(@iscell,a)
ans = 1×4 logical array
0 0 0 1
  댓글 수: 5
the cyclist
the cyclist 2022년 10월 20일
편집: the cyclist 2022년 10월 20일
The image really isn't enough. I can't run the following code -- which exposes the fact the first cell really does contain another cell -- on your image.
load(websave("segs","https://www.mathworks.com/matlabcentral/answers/uploaded_files/1163423/testdata.mat"),"segs");
segs(1)
ans = 1×1 cell array
{1×1 cell}
segs{1}
ans = 1×1 cell array
{'1-a5pre'}
segs{1}{1}
ans = '1-a5pre'
segs(2)
ans = 1×1 cell array
{'.*'}
segs{2}
ans = '.*'
I think you were getting fooled by the fact that the Variable Editor interface seems to look inside 1x1 cells.
Marc Laub
Marc Laub 2022년 10월 20일
Yeah, guess the variable editor shows directly the string within the cell when its a 1x1 cell...

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jiri Hajek
Jiri Hajek 2022년 10월 20일
Hi,
you can use the isa function:
isa(a{i},'cell')
Note that the use of eval is not recommended for many a good reason...
  댓글 수: 2
Marc Laub
Marc Laub 2022년 10월 20일
편집: Marc Laub 2022년 10월 20일
I know that eval is not recommended, but its just for debugging, will not be in the final code.
Somehow
isa
as well as
iscell
gives me a a logical 1 for a{1} and a{3}, but a false for a{2} nd all other muliplicaters, although those are also character arrays.
For the following data, my desired putput would be logical 1 for indices 11,15,17,... and a zero otherwise. Unfortunately; as I said, isa and iscell both give me the wrong output, when the character array start with an operater or dot, such as index 2,4,6 or 8...
Jiri Hajek
Jiri Hajek 2022년 10월 21일
I believe you have got your answer already, as the cyclist pointed out the fact that variable viewer is not a reliable way to check the class of data, rather a viewer that helps you to analyze data. And we can trust the isa/iscell commands, which is not really surprising, considering how long they have been a part of MATLAB...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by