You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Dimensions of arrays being concatenated are not consistent.
6 views (last 30 days)
Show older comments
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.
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
Stephen23
on 10 Apr 2024
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?
Luca Re
on 10 Apr 2024
Edited: Luca Re
on 10 Apr 2024
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
Luca Re
on 10 Apr 2024
Look this :
gg1=load('matlanb_gg1.mat');
Error using load
Unable to find file or directory 'matlanb_gg1.mat'.
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
Voss
on 10 Apr 2024
Edited: Voss
on 10 Apr 2024
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.
Luca Re
on 10 Apr 2024
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?
Voss
on 10 Apr 2024
Edited: Voss
on 10 Apr 2024
"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))
Stephen23
on 10 Apr 2024
Edited: Stephen23
on 10 Apr 2024
"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.
Luca Re
on 10 Apr 2024
>> 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' ?
Stephen23
on 10 Apr 2024
Edited: Stephen23
on 10 Apr 2024
"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.
Image Analyst
on 10 Apr 2024
@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.
Answers (1)
Ramtej
on 10 Apr 2024
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)
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)