Clear Filters
Clear Filters

Failing to create matrix from cell array in several ways

5 views (last 30 days)
לק"י
Hi guys,
I have a cell array in 64X61X21 size.
I want to make a matrix of the values (no vectors) within specific area of the cell array - 15:21,2:61,1:21.
First I make a copy of the cell array:
cellarrayneeded=analysisdayaWT(8:15, 2:60, 1:21);
cellarrayneeded=cell2mat(cellarrayneeded);
Error message I get:
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 118)
ct{mref{:}} = cat(cdim+1,c{mref{:},:});
When I try something else:
cellarrayneeded=cell2mat(analysisdataWT{8:15, 2:60, 1:21});
I get:
Error using cell2mat
Too many input arguments.
What am I doing wrong?
Thanks!
  5 Comments
Stephen23
Stephen23 on 20 Aug 2024
Edited: Stephen23 on 20 Aug 2024
It is very easy to confirm that not all of the data in that cell array are scalar:
S = load('analysisdataWTcopy.mat')
S = struct with fields:
analysisdataWTcopy: {7x60x21 cell}
C = S.analysisdataWTcopy;
X = cellfun(@isscalar,C);
all(X(:)) % nope, not all scalar.
ans = logical
0
nnz(X) % how many are scalar?
ans = 8232
nnz(~X) % how many are non-scalar?
ans = 588
all(cellfun(@isempty,C(~X))) % the non-scalar arrays are all empty
ans = logical
1
Is the range correct? Are the cell array contents correctly defined or imported? We don't know, you need to debug.
Actually looking at your data is much more reliable than relying on what you believe your data to be like.
Amit Ifrach
Amit Ifrach on 20 Aug 2024
לק"י
Hi stephen, thanks!
mmm something indeed is wierd. i'll look it up. it shouldn't be like that.
Thanks!

Sign in to comment.

Answers (1)

Paul
Paul on 19 Aug 2024
Edited: Paul on 19 Aug 2024
Hi Amit,
To subscript into a cell array and return a cell array, use () as with normal array indexing, which should have worked in your first case. We can't see what's contained in your analysisdataWT variable, but it looks like the elements are not all scalars.
% create example cell array
x = rand(64,61,21);
analysisdataWT = num2cell(x);
% pull out subarray and convert to numeric matrix
y = cell2mat(analysisdataWT(15:21,2:61,1:21));
% check
isequal(y,x(15:21,2:61,1:21))
ans = logical
1
  3 Comments
Amit Ifrach
Amit Ifrach on 20 Aug 2024
לק"י
Hi, when I try your mehtod I get this error:
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 118)
ct{mref{:}} = cat(cdim+1,c{mref{:},:});
Stephen23
Stephen23 on 20 Aug 2024
Edited: Stephen23 on 20 Aug 2024
"when I try your mehtod I get this error: Dimensions of arrays being concatenated are not consistent."
This answer does not state that it will fix your incompatibly-sized data.
"... the range I chose contains only scalars."
And yet ... there is always an error with your data. Who do you think is right: you (relying on untested assumptions and beliefs about data) or MATLAB (which uses your actual data)? Once you upload your data then we can help you. And tell us the output from this command:
all(cellfun(@isscalar,analysisdayaWT(8:15, 2:60, 1:21)),'all')

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!