Dimensions of arrays being concatenated are not consistent.
13 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2024-4-10
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
2024-4-10
编辑:Luca Re
2024-4-10
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
2024-4-10
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
2024-4-10
编辑:Voss
2024-4-10
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
2024-4-10
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
2024-4-10
编辑:Voss
2024-4-10
"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
2024-4-10
编辑:Stephen23
2024-4-10
"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
2024-4-10
>> 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
2024-4-10
编辑:Stephen23
2024-4-10
"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
2024-4-10
@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.
回答(1 个)
Ramtej
2024-4-10
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)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)