Clear Filters
Clear Filters

error in concatenating cells

2 views (last 30 days)
ET
ET on 22 Apr 2024
Commented: Walter Roberson on 23 Apr 2024
Hello Dear,
I have 1x50 cell (ftData728178E.trial) in a struct. Each cell has a 192x2301 double. After I concatenate the cell to make a matrix of 192x2301x50, the matrix turns into nan. I wonder why it is and what the correct way to turn cells into a matrix. Thanks,
ET
ft_epoch = cat(3, ftData728178E.trial{:});
  1 Comment
Stephen23
Stephen23 on 23 Apr 2024
Edited: Stephen23 on 23 Apr 2024
"error in concatenating cells"
You are not getting any error. When an error is thrown an error message is displayed and code evaluation stops.
" After I concatenate the cell to make a matrix of 192x2301x50"
You are not concatenating cell arrays. You are confusing the cell array itself with the content of the cell array.
"I wonder why it is and what the correct way to turn cells into a matrix."
It is not possible to "turn" a cell array into a numeric matrix, the concept is basically meaningless. Of course you can easily concatenate the content of cell arrays (assuming compatible sizes and types), using e.g. CELL2MAT or CAT or HORZCAT or VERTCAT or the concatenation operator (concatenation together a comma-separated list).
What you showed in your question:
ft_epoch = cat(3, ftData728178E.trial{:});
What your screenshot shows:
ft_epoch = cell2mat(reshape(ftData728178E.trial, 1 ,1 []));
mean(ft_epoch(1,:,:),3)
Most likely your data have some missing values (e.g. NaN) and you have not checked your data thoroughly.
Upload your data in a MAT file by clicking the paperclip button.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 23 Apr 2024
ft_epoch = cell2mat(reshape(ftData728178E.trial, 1, 1, []));
However, if what you are already doing creates NaN then it means that the doubles are already NaN, and in that case the cell2mat approach will not help.
  2 Comments
ET
ET on 23 Apr 2024
Thanks, Walter, it's strange that while ftData728178E.trial looks normal, it turns into nan after converted into matrix.
Walter Roberson
Walter Roberson on 23 Apr 2024
It shows NaN on the mean(). By default mean() includes NaN values, so if there is a NaN anywhere in the data the result for that page would be NaN.
mean(ft_epoch(1,:,:), 3, "omitmissing")

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!