- write as string data converting to formatted values instead of numeric or
- write each row individually if really leaving blanks is the mandatory requirement of varying-length records.
Accessing contents inside Nested cell
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
dpb
2021년 6월 23일
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
댓글 수: 8
dpb
2021년 6월 24일
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
2021년 6월 24일
댓글 수: 1
dpb
2021년 6월 24일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!