split numbers separated by commas in a sub cell array into their own column

I have a 18x1 cell array filled with numbers. Each cell of the array has a different size. How do I split each cell so there are no longer any subcells. I tried using cell2mat but since each column in the sub cell is of different size it did not work.
This is what the cell array looks like now
image.png
and this is what I am attempting to make it look like
image-1.png

댓글 수: 2

Can you upload the variable in a mat file, using the paper clip icon? An image of the data is not useful for testing.
here you go, I feel like it should be really simple but I have been struggling for a while with this.

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

 채택된 답변

Adam Danz
Adam Danz 2019년 11월 30일
편집: Adam Danz 2019년 11월 30일
You were on the right track with cell2mat but first you have to pad the arrays so they are all equal length. Two of the three lines below use cellfun().
% Max number of elements within each vector
nMax = max(cellfun(@numel,section_2_1));
% Pad all cell elements with 0 so each cell element has equal length
x = cellfun(@(x)[x,zeros(1,nMax-numel(x))],section_2_1,'UniformOutput',false);
% Output matrix
m = cell2mat(x);

댓글 수: 5

Awesome thank you Adam! very helpful
what exactly does the @ do in '@numel' and '@(x)'. I know numel finds number of elements, but I have not seen any documentation as to what the @ does
@ creates an anonymous function. Cellfun applies that function to each element of the cell array.
Here's a different example of an anonymous function
f = @(a,b,c) a + b * c;
y = f(1,3,5)
thank you, you are awesome
Glad I could help!

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2019b

질문:

2019년 11월 30일

댓글:

2019년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by