Accessing contents inside Nested cell

I have a nested cell of the size 1x48 and there are multiple cells inside them as shown below in the screen shot. They are of variable length so some matlab functions like cell2mat is not working.
I want to store those values in a matrix one-by-one as per the order of the cells so that it can be easily written to excel files.
Here is my code that I have tried.
clc
e = edges;
e_size = size(e);
for i=1:1:e_size(1)
for j=1:1:e_size(2)
e_new=e{i,j};
end
end
edges is my nested cell

답변 (2개)

dpb
dpb 2021년 6월 23일

0 개 추천

nMax=cellfun(@numel,edges)
array=cell2mat(cellfun(@(c)cat(2,c,zeros(1,nMax-numel(c))),edges.','UniformOutput',false));
Will augment with zeros, use nan() instead of zeros() for NaN placeholder.
Otherwise, will have to either
  1. write as string data converting to formatted values instead of numeric or
  2. write each row individually if really leaving blanks is the mandatory requirement of varying-length records.

댓글 수: 8

Ketan Shende
Ketan Shende 2021년 6월 23일
Can you help me understand what does c stand for?
dpb
dpb 2021년 6월 23일
Dummy argument in the anonymous function...
Ketan Shende
Ketan Shende 2021년 6월 23일
Well I tried running the code as it is and it gave me the following error.
Error using zeros
Size inputs must be scalar.
Error in Untitled (line 142)
array=cell2mat(cellfun(@(c)cat(2,c,zeros(1,nMax-numel(c))),values.','UniformOutput',false));
Error in Untitled (line 142)
array=cell2mat(cellfun(@(c)cat(2,c,zeros(1,nMax-numel(c))),values.','UniformOutput',false));
Secondly, I replaced zeros with nan and I got the following error
Error using nan
Size inputs must be scalar.
Error in Untitled (line 142)
array=cell2mat(cellfun(@(c)cat(2,c,nan(1,nMax-numel(c))),values.','UniformOutput',false));
Error in Untitled (line 142)
array=cell2mat(cellfun(@(c)cat(2,c,nan(1,nMax-numel(c))),values.','UniformOutput',false));
>> edges={rand(1,4),rand(1,8)} % create a sample cell array...
edges =
1×2 cell array
{1×4 double} {1×8 double}
>> nMax=max(cellfun(@numel,edges))
nMax =
8
>> array=cell2mat(cellfun(@(c)cat(2,c,zeros(1,nMax-numel(c))),edges.','UniformOutput',false))
array =
0.7376 0.8657 0.4787 0.1953 0 0 0 0
0.3574 0.8979 0.2280 0.8536 0.0193 0.3410 0.5375 0.2821
>>
The above edges array mimics the structure of your image; just a little smaller, but that's immaterial.
If you're trying on some other form of input than the above, then we'll have to have the actual form to debug what might be the trouble.
It's always better to attach a .mat file containing the variable rather than images -- nobody can do anything with them other than try to match as did above.
dpb
dpb 2021년 6월 23일
Oh. I see. Indeed you do need to attach edges -- I thought that was what the image was; that's not.
The above will work on as it mimics e_new; it was not intended for another level of nesting.
I'm not up to trying to mimic what the actual input is tonight; attach a .mat file with the content and I'll try to look in the morning again...
Ketan Shende
Ketan Shende 2021년 6월 24일
Here is the mat file.
Ketan Shende
Ketan Shende 2021년 6월 24일
Also, I see in the code that zeros are added. Is there a way to leave it blank if possible?
My code above works for the values array above as is
You can't have a regular array that doesn't fill to the minimum of the largest row with something, no; see the note I wrote in the original answer about that issue.
But, writecell can handle that case of singly-stored cells--
writecell(values.','Values.xls','UseExcel',1)

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

Ketan Shende
Ketan Shende 2021년 6월 24일

0 개 추천

Now I am trying to convert another nested cell that I am attaching here into the matrix by following the same code. I am getting the following error.
Here is the code:
p_nMax=max(cellfun(@numel,P));
P_array=cell2mat(cellfun(@(c)cat(2,c,zeros(1,p_nMax-numel(c))),P.','UniformOutput',false));
Error:
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in W23rdJune>@(c)cat(2,c,zeros(1,p_nMax-numel(c))) (line 113)
P_array=cell2mat(cellfun(@(c)cat(2,c,zeros(1,p_nMax-numel(c))),P.','UniformOutput',false));
Error in W23rdJune (line 113)
P_array=cell2mat(cellfun(@(c)cat(2,c,zeros(1,p_nMax-numel(c))),P.','UniformOutput',false));

댓글 수: 1

Well, P doesn't follow the form --
> P
P =
1×4 cell array
{1×48 cell} {1×48 cell} {1×48 cell} {48×40 double}
of a cell array of doubles -- the last cell is a 2D double array instead of a vector of a cell.
You can make the catenation work to the form of the cell by changing the definitions above to use the appropriate size() arguments in place of the assumed 1 for size(_,1) and numel() for size(_,2) but the double array instead of the cell array will break the cell2mat operation.
You need the consistent way to generate all the cells in the array; whatever process you used for the last is inconsistent with the rest.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2021년 6월 23일

댓글:

dpb
2021년 6월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by