Clear Filters
Clear Filters

Cell of Strings, using cell2mat gives Dimensions of arrays being concatenated are not consistent.

65 views (last 30 days)
I've got an excel document with lots of fancy editing that I'm trying to do processing on.
I've imported it as a table since rows may change over time and I'd like to be able to use row names to reference table data. I need to make some changes to the first column in order to use T.Properties.RowNames
Here's been the process so far, Import as table > grab the first column with table2array > this gives an array of cells > replace all the 0x0 char empty cells with a unique string > use cell2mat to convert to an array of strings > pass string array to T.Properties.RowNames
however, when I go to use cell2mat I get the error "Dimensions of arrays being concatenated are not consistent."
can't figure out what's causing it, I tried using which was a fix I found elsewhere to check for empty cells that may be the cause:
isEm = cellfun( @isempty, namerows ) ;
data = cell2mat( namerows(~isEm) ) ;
but the cell2mat function in this fix returns the same error
Any help would be appreciated
Here's my code:
%impot table, extract first column, convert to an array of cells
T = readtable('3-22 week 3.xlsx');
namerows = T(:,1);
namerows = table2array(namerows);
%array of numbers to be passed to sprintf to ensure each row has a unique
%name
nullname = 1:length(namerows);
%replace any unnamed rows with a string
for n = 1:length(namerows)
if (namerows{n,1} == "")
namerows{n,1} = sprintf('null%d',nullname(n))
end
end
%double check there are no empty cells
isEm = cellfun( @isempty, namerows ) ;
data = cell2mat( namerows(~isEm) ) ; %CODE FAILS HERE
namerows = cell2mat(namerows)

Accepted Answer

Voss
Voss on 14 Apr 2022
I'm not sure why you want to use cell2mat to convert a cell array of character vectors to an array of strings. You can use string for that:
% cell array of character vectors:
namerows = repmat({'name'},10,1);
namerows{4} = ''; % some are empty
%replace any unnamed rows with a string
nullname = 1:length(namerows);
for n = 1:length(namerows)
if (namerows{n,1} == "")
namerows{n,1} = sprintf('null%d',nullname(n));
end
end
disp(namerows) % none are empty -> removing empties before cell2mat won't matter
{'name' } {'name' } {'name' } {'null4'} {'name' } {'name' } {'name' } {'name' } {'name' } {'name' }
try
namerows = cell2mat(namerows)
catch ME
disp(ME.message);
namerows = string(namerows)
end
Dimensions of arrays being concatenated are not consistent.
namerows = 10×1 string array
"name" "name" "name" "null4" "name" "name" "name" "name" "name" "name"
cell2mat on a cell array of character vectors will attempt to concatenate the character vectors, so they have to have consistent sizes:
cell2mat({'this_';'is_ok'})
ans = 2×5 char array
'this_' 'is_ok'
cell2mat({'this_';'is_not'})
Error using cat
Dimensions of arrays being concatenated are not consistent.

Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
  2 Comments
Sage Hayes
Sage Hayes on 14 Apr 2022
Thank you! solved. Didn't realize that the contents of each cell were of type char, I thought each cell was a string.
Voss
Voss on 14 Apr 2022
You're welcome!
If it had been a cell array of strings, you would've gotten a different error (but using string to convert to a string array would still work):
% cell array of *strings*:
namerows = repmat({"name"},10,1);
namerows{4} = ""; % some are empty
%replace any unnamed rows with a string
nullname = 1:length(namerows);
for n = 1:length(namerows)
if (namerows{n,1} == "")
namerows{n,1} = sprintf('null%d',nullname(n)); % this is a character vector
end
end
disp(namerows) % some strings, some character vectors
{["name"]} {["name"]} {["name"]} {'null4' } {["name"]} {["name"]} {["name"]} {["name"]} {["name"]} {["name"]}
try
% the error you'd get here is:
% "All contents of the input cell array must be of the same data type."
namerows = cell2mat(namerows)
catch ME
disp(ME.message);
% this works the same whether it's a cell array of character vectors or
% strings (or some of both)
namerows = string(namerows)
end
All contents of the input cell array must be of the same data type.
namerows = 10×1 string array
"name" "name" "name" "null4" "name" "name" "name" "name" "name" "name"

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!