Dimensions of arrays being concatenated are not consistent.
Ältere Kommentare anzeigen
hi, i receive this error...How can i solve it?
arr = load('matlab_bb.mat')
disp(arr.bb)
cell2mat(arr.bb)
cell2mat(bb)
Error using cat
Dimensions of arrays being concatenated are not consistent.
13 Kommentare
Dyuman Joshi
am 10 Apr. 2024
"How can i solve it?"
That depends on what you want to do with the data.
Stephen23
am 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?
gg1 = load('matlab_gg1.mat').gg_1;
gg2 = load('matlab_gg2.mat').gg_2;
They are not equal:
isequal(gg1,gg2)
For one thing, they are different sizes:
whos gg*
Also, cell array gg2 contains only double (numeric) arrays:
unique(cellfun(@class,gg2,'UniformOutput',false))
but some cell(s) in cell array gg1 contain a logical array:
unique(cellfun(@class,gg1,'UniformOutput',false))
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.
shamal
am 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))
"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.
shamal
am 10 Apr. 2024
shamal
am 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.
shamal
am 10 Apr. 2024
Image Analyst
am 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.
Antworten (1)
Ramtej
am 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)
Kategorien
Mehr zu Cell Arrays finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!