Dimensions of arrays being concatenated are not consistent.

hi, i receive this error...How can i solve it?
arr = load('matlab_bb.mat')
arr = struct with fields:
bb: {76x1 cell}
disp(arr.bb)
{'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Mini' } {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'} {'On Micro'}
cell2mat(arr.bb)
Error using cat
Dimensions of arrays being concatenated are not consistent.

Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
cell2mat(bb)
Error using cat
Dimensions of arrays being concatenated are not consistent.

13 Comments

"How can i solve it?"
That depends on what you want to do with the data.
You are trying to vertically concatenate character vectors which have different lengths. Of course that throws an error.
"How can i solve it?"
What exactly is there to "solve" ? What do you expect the output to be?
So I created a structure made with cells... in this structure I inserted numbers and strings I have to load the .list file into another matlab app (closed source) but it gives me this error:" Error using cell2mat All contents of the input cell array must be of the same data type". So I should convert my structure so that the "cell2mat" works correctly for me
Look this :
gg1=load('matlanb_gg1.mat');
Error using load
Unable to find file or directory 'matlanb_gg1.mat'.
gg2=load('matlanb_gg2.mat');
gg1
{[1]}
{[1]}
{[1]}
{[1]}
{[1]}
{[1]}
{[1]}
{[1]}
gg2
{[1]}
{[1]}
{[1]}
{[1]}
{[1]}
{[1]}
{[1]}
{[1]}
are equals but :
>> cell2mat(gg_1)
Error using cell2mat
All contents of the input cell array must be of the same data type.
cell2mat(gg_2)
ans =
1
1
1
1
are equal but have different behaviors
gg1 = load('matlab_gg1.mat').gg_1;
gg2 = load('matlab_gg2.mat').gg_2;
They are not equal:
isequal(gg1,gg2)
ans = logical
0
For one thing, they are different sizes:
whos gg*
Name Size Bytes Class Attributes gg1 76x1 8001 cell gg2 112x1 12544 cell
Also, cell array gg2 contains only double (numeric) arrays:
unique(cellfun(@class,gg2,'UniformOutput',false))
ans = 1x1 cell array
{'double'}
but some cell(s) in cell array gg1 contain a logical array:
unique(cellfun(@class,gg1,'UniformOutput',false))
ans = 2x1 cell array
{'double' } {'logical'}
That's why you cannot use cell2mat on gg1. Recall the error message: All contents of the input cell array must be of the same data type.
okk excsue me for different size ...I messed up with the data export
but about type data i see
Name Size Bytes Class Attributes
gg1 76x1 8001 cell
gg2 112x1 12544 cell
how do you see that one is double and the other is logical?
"how do you see that one is double and the other is logical?"
They are both cell arrays, but some cells of gg1 contain a logical array (and some contain a numeric (double) array), whereas all cells of gg2 contain a numeric (double) array.
To see that, I used the class function. Specifically, I used cellfun to call class on each cell's contents, to get the data type of each cell's contents:
cellfun(@class,gg1,'UniformOutput',false)
and then took only the unique set of data types contained in the cell array:
unique(cellfun(@class,gg1,'UniformOutput',false))
"how do you see that one is double and the other is logical?"
A cell array is not a double nor a logical. A cell array is a cell array.
Cell arrays are container arrays: they contain other arrays. They can contain double, logical, and other array types.
Voss's comment showed you exactly how they checked the content of those two cell arrays. Read their comment again.
>> class(gg1(1))
ans =
'cell'
>> cellfun(@class,gg1(1),'UniformOutput',false)
ans =
1×1 cell array
{'double'}
but why with class i get 'cell' and cellfun i get 'double' ?
"but why with class i get 'cell' and cellfun i get 'double' ?"
Because indexing a cell array using parentheses returns another cell array, NOT its content:
Exactly as the documentation states, use curly braces if you want to access the content of a cell array:
class(gg1{1})
% ^ ^ curly braces to access cell CONTENT
By the way, in MATLAB indexing using parentheses always returns an array of the same type, never the content. For container classes (e.g. cell, string, table) curly braces refers to the content. Simple and consistent.
@Luca Re see the FAQ for a good explanation of a cell array:
It explains how and when to use curly braces, square bracket, or round parentheses. I think it will help you get a good intuitive feel for when to use each.

Sign in to comment.

Answers (1)

Hi,
Assuming you are triying to convert cell array of characters into string array.
You can use "string" function for your case as shown below.
stringMatrix = string(arr.bb)

Asked:

on 10 Apr 2024

Commented:

on 10 Apr 2024

Community Treasure Hunt

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

Start Hunting!