Convert a cell array that contains numbers and NaN values to a matrix

I have a cell array Acceleration {131341x1} that contains NaN and numbers. I tried to use the cell2mat like
Acc = cell2mat (Acceleration);
but I get following error:
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
Any idea how can I solve the problem?
Thanks!

Answers (1)

The elements of the cell do not have the same size:
W = cellfun('ndims', Acceleration);
isequal(length(unqiue(W)), 1)
S = cellfun('size', Acceleration, 2);
unique(S)
If you have different number of columns, how should the result look like? It does not matter if the values contain NaNs, but only if the sizes match.
Are the cell elements vectors? Then you could pad them with NaNs, see FEX: padcat.

4 Comments

I don't quite understand how the code can be helpful for my case. I tried it with my data and at the end I get matrix S as double (131342x1) with all the elements equal to 3.
The cells within Acceleration that have NaN are e.g. Acceleration{1,1} = NaN. The cells with numbers look like are Acceleration {5000,1} = 3560.453. Every cell from {1,1} to {131341,1} is a 1x1.
What do these output?:
unique(cellfun('size', Acceleration, 1))
unique(cellfun('size', Acceleration, 2))
I didn't look careful enough on the data I have. So instead of NaN I have NAN so the dimension is {1x3}. The cell elements that contain numbers are {1x1}.
The first cell element is Acceleration{1,1} = NAN and NAN continue until Acceleration{44811,1} = NAN. Then I have numeric variables from cell element {44812,1} to cell element {131341,1}.
Now I need to convert NAN to NaN and cell2mat function will work. Any idea how to convert NAN to NaN?
Thanks!
OK, I found a solution. I used this line of code and automatically I got a matrix (131341x1).
Acc = str2num(strvcat(strrep(Acceleration,'NAN','NaN')));
Anyway, thank you for all the help.
Cheers!

Sign in to comment.

Categories

Asked:

EB
on 5 Apr 2017

Commented:

EB
on 6 Apr 2017

Community Treasure Hunt

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

Start Hunting!